简体   繁体   English

ASP.NET Core 2部分视图列表模型绑定

[英]ASP.NET Core 2 Partial View List Model binding

I have a model which has a list of models public List<BaseModuleModel> modules { get; set; } 我有一个模型,其中有一个模型列表public List<BaseModuleModel> modules { get; set; } public List<BaseModuleModel> modules { get; set; }

In my razor file I do a for each loop on all of these modules and then generate the partial view of them and pass in the model. 在我的剃刀文件中,对所有这些模块的每个循环都执行一个a,然后生成它们的部分视图并传入模型。

@foreach (BaseModuleModel module in Model.modules)
        {
            <div class="section row col-lg-8">
                <div class="section-header">
                    <p class="header-text">@module.PartialName</p>
                </div>
                <div class="row col-lg-12 section-content">
                    @await Html.PartialAsync("~/Views/Partials/" + module.PartialName + "Partial.cshtml", module)
                </div>
            </div>
        }

This all works as intended. 这一切都按预期工作。 But when I submit my form the partial view does not change any variables of the BaseModuleModels in the list and all of them still have all variables set to null. 但是,当我提交表单时,局部视图不会更改列表中BaseModuleModels任何变量,并且所有变量仍将所有变量设置为null。

How do I make it so even these partial views bind when I click submit my form. 当我单击提交表单时,如何使这些局部视图绑定在一起。

Partial view: 部分视图:

@model Lidmaatschap.Models.modules.VoorwaardenModule

<div class="row col-lg-12 section-content">
    <div class="col-lg-6">
        <label asp-for="PrivacyStatement" class="control-label"></label><br />
        <input asp-for="PrivacyStatement" type="checkbox" value="false" /> Ik ga akkoord met de privacy voorwaarden.
        <span asp-validation-for="PrivacyStatement" class="text-danger"></span>
    </div>
    <div class="col-lg-6">
        <label asp-for="FirstName" class="control-label"></label>
        <input asp-for="FirstName" class="form-control wide-full" />
        <span asp-validation-for="FirstName" class="text-danger"></span>
    </div>
</div>

The Model class: 模型类:

public class VoorwaardenModule : BaseModuleModel
    {
        public VoorwaardenModule() : base("Voorwaarden")
        {

        }

        [Display(Name = "Privacy Verklaring")]
        //[Range(typeof(bool), "true", "true", ErrorMessage = "Je moet akkoord gaan met het privacy beleid om door te gaan.")]
        public bool PrivacyStatement { get; set; }

        [Display(Name = "Voornaam")]
        [Required]
        public string FirstName { get; set; }
    }

My Index.cshtml: https://pastebin.com/ErXcJz0a 我的Index.cshtml: https ://pastebin.com/ErXcJz0a

MemberModel.cs : https://pastebin.com/KaSVUV6Q MemberModel.cshttps : MemberModel.cs

HomeController.cs : https://pastebin.com/YNxJPvmX HomeController.cshttps : //pastebin.com/YNxJPvmX

The partial view needs the parent context to name the form fields properly. 局部视图需要父上下文来正确命名表单字段。 By default, it's just going to use the property name of the model that's passed into, so your PrivacyStatement -bound input will have name="PrivacyStatement" , when what you actually need is name="modules[N].PrivacyStatement" , in order to properly bind it back on post. 默认情况下,它将仅使用传入的模型的属性名称,因此,在您实际需要的是name="modules[N].PrivacyStatement" ,您的PrivacyStatement绑定输入将具有name="PrivacyStatement" 。以便将其正确地重新装回岗位。 As such, you have two issues. 因此,您有两个问题。

First, you cannot use foreach . 首先,您不能使用foreach The partial view is going to need the index of the item in the list, which means you need to actually pass it an indexed item, ie modules[N] . 部分视图将需要列表中项目的索引,这意味着您实际上需要将其传递给索引项目,即modules[N] Second, you need to supply the HtmlFieldPrefix from the parent view, while that's possible to do with Html.PartialAsync , utilzing the viewData param, it's quite arduous and ugly code. 其次,您需要从父视图中提供HtmlFieldPrefix ,尽管这可以与Html.PartialAsync一起Html.PartialAsync ,利用viewData参数,但是这是很繁琐的代码。 It's far better to use the <partial> tag helper (which the docs recommend anyways) and its for attribute. 最好使用<partial>标记帮助器(无论如何文档会推荐使用)及其for属性。

Long and short, change you code to: 总而言之,将您的代码更改为:

@for (var i = 0; i < Model.modules.Count; i++)
{
    <div class="section row col-lg-8">
        <div class="section-header">
            <p class="header-text">@Model.modules[i].PartialName</p>
        </div>
        <div class="row col-lg-12 section-content">
            <partial name="~/Views/Partials/@(Model.modules[i].PartialName)Partial.cshtml" for="modules[i]" />
        </div>
    </div>
}

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

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