How to persist data models passed to partial views?

后端 未结 2 954
甜味超标
甜味超标 2020-11-30 13:27

To illustrate the problem I face, I have put together three simple data models:

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


        
2条回答
  •  温柔的废话
    2020-11-30 13:39

    Pass the OverallModel to your partials so that the controls will be correctly named for posting back to OverallModel. Currently you would have controls with name="FirstName" but they need to be name="Personal.FirstName"

    @Html.Partial("Personal", Model)
    

    and change the partials to suit

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

    As an alternative, you can also pass the prefix to the partial

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

提交回复
热议问题