在将模型发布到控制器之前,我有以下代码来填充我的模型。 我正在迭代的列表是一个列表。
<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; }
}
答案 0 :(得分:3)
用于生成集合中元素的代码需要
@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"
)
如果您无法更改要实施的集合EditorTemplate
C_CategoryDescription
模型使用自定义IList
在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)