简体   繁体   中英

Hidden field not storing value from partial view into an array of complex object

I am unable to get an array of a complex type in the model to store its value IsAssigned between two actions in a controller.

Once the model has been composed, which including the array of complex type (which include IsAssigned), the view engine begins to render the HTML/Razor script. In the view, there is a section of the script that handles the displaying of the controls which manipulates the complex type. It is a "For i" loop that cycles through the array of complex type.

During this loop, there is a razor command HiddenFor for property IsAssigned which is contained inside of the complex type array.

A second step is carried out in the loop that renders a partial view. It is in this view where there are two radio buttons linked to an array position's IsAssigned boolean? property. If the user select yes the property turns to true, and no false.

After the view has been completely rendered and the user has made their selections, the next action method in the controller is activated and the model is passed to it. It is here where I expect the user choices against the IsAssigned property to persist, but no. It does not.

Below is the code for looping and the code for the partial view.

Loop

    @for (int i = 0; i < Model.KitTypeAssignment.Length; i++)
{
    @Html.HiddenFor(m => m.KitTypeAssignment[i].IsKitTypeActiveForSubject)
    @Html.HiddenFor(m => m.KitTypeAssignment[i].KitType.Code)
    @Html.HiddenFor(m => m.KitTypeAssignment[i].KitType.Description)
    @Html.HiddenFor(m => m.KitTypeAssignment[i].KitType.KitTypeId)

    @Html.HiddenFor(m => m.KitTypeAssignment[i].IsAssigned, new { @Name = "IsAssigned" + Model.KitTypeAssignment[i].KitType.Code })

    @Html.Partial("_KitTypeDispensing", Model.KitTypeAssignment[i])
}

PartialView

    <div class="field inline spacer20">
@{

    var kitTypeAssign = String.Format("{0}{1}", Model.KitType.Code, "Assign");
    var identityOfAssigned = new { id = @kitTypeAssign, @Name = "IsAssigned" + Model.KitType.Code };

    var kitTypeNoAssign = String.Format("{0}{1}", Model.KitType.Code, "NoAssign");
    var identityOfNoAssigned = new { id = @kitTypeNoAssign, @Name = "IsAssigned" + Model.KitType.Code };

    var sectionTitle = string.Format("{0} {1}", PromptText.Assign_185B7133DB22230701A857C059360CC2.ToLocalizableString(), Model.KitType.Description);

    @Html.Label(sectionTitle, new { @class = "colon" })
    @Html.RadioButtonFor(m => m.IsAssigned, true, @identityOfAssigned)
    <label for=@kitTypeAssign>@Html.PromptFor(PromptText.Yes_93CBA07454F06A4A960172BBD6E2A435)</label>
    @Html.RadioButtonFor(m => m.IsAssigned, false, @identityOfNoAssigned)
    <label for=@kitTypeNoAssign>@Html.PromptFor(PromptText.No_BAFD7322C6E97D25B6299B5D6FE8920B)</label>
}
</div>

I think the problem is that the partial view does not prefix the names of its HTML input elements correctly, therefore the ModelBinder does not know how to bind them back to your array. Try to pass the correct prefix explicitly to the partial:

 @Html.Partial(
     "_KitTypeDispensing", 
     Model.KitTypeAssignment[i], 
     new ViewDataDictionary { TemplateInfo = new TemplateInfo { 
         HtmlFieldPrefix = Html.NameFor(m => m.KitTypeAssignment[i]).ToString() } 
     }
 )

Further reading: ASP.NET MVC Partial Views with Partial Models

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM