MVC 帖子具有复杂类型,模型为空

本文关键字:模型 类型 复杂 MVC | 更新日期: 2023-09-27 18:33:36

在将模型发布到控制器之前,我有以下代码来填充我的模型。我正在迭代的列表是一个列表。

<div class="tab-content">
            @*@foreach (var description in Model.Category.C_CategoryDescription)*@
            @for (var i = 0; i < Model.Category.C_CategoryDescription.Count; i++)
            {
                <div id="@("tab" + @Model.Category.C_CategoryDescription.ToList()[i].ProductTypeId)" class="@(Model.Category.C_CategoryDescription.ToList()[i] == @Model.Category.C_CategoryDescription.First() ? "tab-active" : "tab")">
                    <div class="form-group ">
                        @Html.LabelFor(model => model.Category.C_CategoryDescription.ToList()[i].DescriptionTop, "Beskrivelse - Top", htmlAttributes: new {@class = "control-label col-md-2"})
                        <div class="col-md-10">
                            @Html.TextAreaFor(model => model.Category.C_CategoryDescription.ToList()[i].DescriptionTop, new {@class = "richText"})
                        </div>
                    </div>
                    <div class="form-group">
                        @Html.LabelFor(model => model.Category.C_CategoryDescription.ToList()[i].DescriptionBottom, "Beskrivelse - Bund", htmlAttributes: new {@class = "control-label col-md-2"})
                        <div class="col-md-10">
                            @Html.TextAreaFor(model => model.Category.C_CategoryDescription.ToList()[i].DescriptionBottom, new {@class = "richText"})
                        </div>
                    </div>
                </div>
            }
        </div>

HTML出来很好。但是一旦我在控制器中抓住帖子,模型就是空的。不是空,而是空。我读过很多文章,说它指出了模型绑定的问题。

我更改了我的代码以反映所描述的内容:这里

还是没有骰子。任何帮助,不胜感激。

编辑:我根据这篇文章更改了我的代码。

我的观点现在如下所示:

<div class="tab-content">
            @Html.Partial("_Edit", Model.Category.C_CategoryDescription.ToList())
        </div>

部分视图如下所示:

    @model IList<DataAccess.Plusbog.C_CategoryDescription>
@{
    var productType = Model;
}
@for (var i = 0; i < productType.Count; i++)
{
    <div id="@("tab" + @Model[i].ProductTypeId)" class="@(Model[i] == @Model.First() ? "tab-active" : "tab")">
        <div class="form-group ">
            @Html.LabelFor(model => productType[i].DescriptionTop, "Beskrivelse - Top", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextAreaFor(model => productType[i].DescriptionTop, new { @class = "richText" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => productType[i].DescriptionBottom, "Beskrivelse - Bund", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextAreaFor(model => productType[i].DescriptionBottom, new { @class = "richText" })
            </div>
        </div>
    </div>
}

可悲的是,同样的结果。

编辑:

以下是模型:

public class CategoryModel
{
    public C_Category Category { get; set; }
    public SelectList Categories { get; set; }
    public SelectList ProductTypes { get; set; }
    public String ISBNListToAddManually { get; set; }
    public string Response { get; set; }
}

和C_Category类:

public partial class C_Category
{
    public C_Category()
    {
        this.C_CategoryDescription = new HashSet<C_CategoryDescription>();
        this.Books = new HashSet<Books>();
        this.ChildCategories = new HashSet<C_Category>();
        this.Campaign = new HashSet<Campaign>();
        this.Group = new HashSet<Group>();
    }
    public int Id { get; set; }
    public Nullable<int> ParentCategoryId { get; set; }
    public string Name { get; set; }
    public bool Active { get; set; }
    public string Slug { get; set; }
    public string Keywords { get; set; }
    public virtual ICollection<C_CategoryDescription> C_CategoryDescription { get; set; }
    public virtual ICollection<Books> Books { get; set; }
    public virtual ICollection<C_Category> ChildCategories { get; set; }
    public virtual C_Category ParentCategory { get; set; }
    public virtual ICollection<Campaign> Campaign { get; set; }
    public virtual ICollection<Group> Group { get; set; }
}

最后,C_CategoryDescription:

public partial class C_CategoryDescription
{
    public int CategoryId { get; set; }
    public int ProductTypeId { get; set; }
    public string DescriptionTop { get; set; }
    public string DescriptionBottom { get; set; }
    public string MetaDescription { get; set; }
    public string MetaKeywords { get; set; }
    public string AlternativeTitle { get; set; }
    public virtual C_Category C_Category { get; set; }
    public virtual C_ProductType C_ProductType { get; set; }
}

MVC 帖子具有复杂类型,模型为空

用于生成集合中元素的代码需要

@Html.TextAreaFor(m => m.Category.C_CategoryDescription[i].DescriptionTop, new {@class = "richText"})

这将生成正确的name属性

<textarea name="Category.C_CategoryDescription[0].DescriptionTo" ... />
<textarea name="Category.C_CategoryDescription[1].DescriptionTo" ... />

您当前使用的.ToList()正在生成不正确的name属性(未经测试,但我认为它name="[0].DescriptionTo"

或者,如果无法更改集合以实现IList,则可以对C_CategoryDescription模型使用自定义EditorTemplate

Views/Shared/EditorTemplates/C_CategoryDescription.cshtml(注意文件名必须与类的名称匹配)

@model yourAssembly.C_CategoryDescription
<div class="form-group ">
    @Html.LabelFor(m => m.DescriptionTop, "Beskrivelse - Top", new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.TextAreaFor(m => m.DescriptionTop, new { @class = "richText" })
    <div>
</div>
....

然后在主视图中,为集合中的每个项目生成正确的 HTML

@Html.EditorFor(m => m.Category.C_CategoryDescription)