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
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.