MVC Post 具有复杂类型,模型为空

MVC Post with complex types, model is empty

我有以下代码来填充我的模型,然后 post将它发送到我的控制器。 我正在遍历的列表是一个列表。

<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 结果很好。但是一旦我在我的控制器中捕捉到 post ,模型就为空。不是 NULL,而是空的。 我看了很多文章说它指出了模型绑定的问题。

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

仍然没有骰子。 感谢任何帮助。

编辑:我根据 this post.

更改了我的代码

我的视图现在是这样的:

<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 class:

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_Category描述:

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; }
}

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

@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中(注意文件名必须与class中的名称匹配)

@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)