I have a scenario in which I would like to display data for users and let them change it. For this, I have a form that is set with the current values โโof the object. This form is enclosed as a partial view, which in turn is called in a loop depending on how many children my parent has.
I also provide an empty form at the end so that users can create new instances of this child, so, in short, I have something like this:
Change form (partial view):
@model WebPortal.Models.AddressModel
@using (Html.BeginForm("EditAddress", "MerchantDetailsEditor", FormMethod.Post))
{
@Html.ValidationSummary(true)
@Html.LabelFor(model => model.StreetNumber, new { id = Model.ID + "_streetnumber_label", Name = Model.ID + "_streetnumber_label", @for = Model.ID + "_streetnumber" })
@Html.TextBoxFor(address => address.StreetNumber, new { id = Model.ID + "_streetnumber", Name = Model.ID + "_streetnumber"})
@Html.ValidationMessageFor(address => address.StreetNumber)
Create a form (another partial view)
@model WebPortal.Models.AddressModel
@{
int counter = 1;
}
@using (Html.BeginForm("AddAddress", "MerchantDetailsEditor", FormMethod.Post))
{
@Html.ValidationSummary(true)
@Html.LabelFor(model => model.StreetNumber, new { id = counter + "_streetnumber_label", Name = counter + "_streetnumber_label", @for = counter + "_streetnumber" })
@Html.TextBoxFor(address => address.StreetNumber, new { id = counter + "_streetnumber", Name = counter + "_streetnumber"})
@Html.ValidationMessageFor(address => address.StreetNumber)
counter++;
Which in turn is called like this:
@foreach (WebPortal.Models.AddressModel address in @Model.Addresses)
{
@Html.Partial("Edit_Form", address)
}
@Html.Partial("Add_Form", new WebPortal.Models.AddressModel())
, , , , , , . , , , ( ) .
, , MVC ID , , , .
, ID Name, .
, , . , , , , . - / .
!