【问题标题】:MVC Post with complex types, model is empty具有复杂类型的 MVC Post,模型为空
【发布时间】:2016-02-08 20:34:57
【问题描述】:

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

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

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

仍然没有骰子。 任何帮助表示赞赏。

编辑:我根据this 帖子更改了我的代码。

我的视图现在看起来像这样:

<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_CategoryDe​​scription:

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 =&gt; m.Category.C_CategoryDescription[i].DescriptionTop) - 查看您当前生成的 html 的 name 属性以了解(并且您的集合需要实现 IList&lt;T&gt; 否则您需要使用自定义EditorTemplate)
  • 我多次遇到同样的问题......最后我决定在每个视图中只提供或多或少的静态页面,并使用 Json 和 AJAX 请求/帖子处理所有与数据相关的内容。这不仅极大地改善了用户体验,还让我的生活方式更轻松,开发速度更快。

标签: c# asp.net-mvc asp.net-mvc-4 model-binding


【解决方案1】:

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

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

【讨论】:

  • 我现在已经实现了局部视图,因为我使用的是 ICollection,并且是 ICollection 类型,因为它是实体的导航属性。查看我更新的代码。
  • 你不能使用部分。同样,查看您为 name 属性生成的 html - 它与您的模型无关。如果您无法收集IList&lt;T&gt;,那么您可以使用EditorTemplate。重命名部分以匹配您的集合中使用的类的名称(我假设它将是C_CategoryDescription.cshtml)并将其放在/Views/Shared/EditorTemplates 文件夹中,然后在主视图中使用@Html.EditorFor(m =&gt; m.Category.C_CategoryDescription)
  • 看看你的部分,似乎你在一个集合中有一个集合,所以你需要 2 个编辑器模板,一个调用另一个。您确实需要展示所有模型,以便我为您提供正确的代码。
  • 更新模型 :-)
  • 好的,它不是嵌套集合,但我会很快用EditorTemplate 选项更新答案,它只需要IEnumerable 而不是IList(但说真的,你不应该使用数据模型就像在你的视图中一样 - 而是使用视图模型)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多