How to transfer data models to partial views?

To illustrate the problem I am facing, I have compiled three simple data models:

 public class PersonalModel {
     public string FirstName { get; set; }
     public string LastName { get; set; }
 }

 public class AddressModel {
     public string Street { get; set; }
 }

 public class OverallModel {
    public string Id { get; set; }
    public PersonalModel Personal { get; set; }
    public AddressModel Address { get; set; }
 }

Here is my simple Index.chtml:

@model WebAppTest.Models.OverallModel
@using (Html.BeginForm())
{
  @Html.AntiForgeryToken()

  <div class="form-horizontal">
    <h4>OverallModel</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.Id)

    <div>
        @Html.Partial("Personal", Model.Personal)
    </div>
    <div>
        @Html.Partial("Address", Model.Address)
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
  </div>
}

When the "Save" button is clicked, the following controller method is called:

[HttpPost]
public ActionResult Index(OverallModel model) {
    return View(model);
}

The problem is that the values model.Personaland are model.Addressalways displayed as null in the controller method.

Although the values ​​are correctly transferred to partial views, the Razor mechanism cannot combine a common object when the submit button is pressed.

If you could enlighten me on what I am missing, I would appreciate it. Regards.

0
source share
2 answers

OverallModel , OverallModel. name="FirstName", name="Personal.FirstName"

@Html.Partial("Personal", Model)

@model OverallModel
....
@Html.TextBoxFor(m => m.Personal.FirstName)

@Html.Partial("Address", Model.Personal, new ViewDataDictionary { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "Personal" }})
+6

, , 2 - .

- EditorTemplates.

aka Personal, Address, Overal.

( ) Overal Get, View Post, .

 public ActionResult Index()
    {
        var model = new OveralModel();

        return View(model);
    }

    [HttpPost]
    public ActionResult Index(OveralModel model)
    {
        return View(model);
    }

( )

@model EditorFor.Models.OveralModel

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>OverallModel</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(m => m.Id)

        <div>            
            @Html.EditorFor(m => m.Personal)
        </div>
        <div>
            @Html.EditorFor(m => m.Address)
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

PartialView EditorModels, PartialView, , .

, , EditorTemplates Views/Shared Views , .

EditorTemplate PersonalModel PersonalModel.cshtml, .

@model EditorFor.Models.PersonelModel


<div class="form-group">
    @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
    </div>
</div>

, sou @Html.EditorFor(m => m.Personal), EditorTemplates EditorTemplate .

"",

, , , .

PS: , , .

0

All Articles