asp.net mvc parent child view with parent view updating children

后端 未结 2 587
暖寄归人
暖寄归人 2020-12-07 04:38

I am using a partial view to create a parent child view. What I would ideally like is the submit button on the parent view to save the child values.

I have the follo

2条回答
  •  [愿得一人]
    2020-12-07 04:45

    Your foreach loop is generating duplicate id attributes (invalid html) and name attributes which have no relationship to your model. Change the partial to an EditorTemplate

    /Views/Shared/EditorTemplates/CourseHole.cshtml

    and remove the BeginForm, AntiForgeryToken and scripts

    @model Golf_Statz.Models.CourseHole
    

    @Model.Number

    @Html.EditorFor(model => model.Par, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Par, "", new { @class = "text-danger" })
    @Html.EditorFor(model => model.Length, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Length, "", new { @class = "text-danger" })
    @Html.EditorFor(model => model.Ranking, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Ranking, "", new { @class = "text-danger" })

    and then in the main view

    @Html.EditorFor(m => m.Holes)
    

    The EditorFor() method will correctly generate the html for binding to a collection, for example

    
    
    

    You also need to remove the [Bind] attribute since you are excluding property Holes, and you seem to want to bind to all properties anyway.

    Side note: You do not generate an input for the hole CourseHoleId or Number properties so these wont post back.

提交回复
热议问题