简体   繁体   English

隐藏字段不将部分视图的值存储到复杂对象的数组中

[英]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. 我无法在模型中获取复杂类型的数组来在控制器的两个动作之间存储其值IsAssigned。

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. 一旦组成了模型(包括复杂类型的数组(包括IsAssigned)),视图引擎就开始呈现HTML / Razor脚本。 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. 这是一个“ For i”循环,循环遍历复杂类型的数组。

During this loop, there is a razor command HiddenFor for property IsAssigned which is contained inside of the complex type array. 在此循环期间,复杂属性数组内部包含一个针对IsAssigned属性的剃刀命令HiddenFor。

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? 在此视图中,有两个单选按钮链接到阵列位置的IsAssigned布尔值吗? property. 属性。 If the user select yes the property turns to true, and no false. 如果用户选择是,则属性变为true,否则为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. 在这里,我希望用户对IsAssigned属性的选择将继续存在,但不会。 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. 我认为问题在于局部视图无法正确为其HTML输入元素的名称添加前缀,因此ModelBinder不知道如何将其绑定回数组。 Try to pass the correct prefix explicitly to the partial: 尝试将正确的前缀显式传递给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 进一步阅读: 具有局部模型的ASP.NET MVC局部视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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