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