无法绑定模型类型的集合

时间:2015-01-23 02:11:33

标签: asp.net-mvc asp.net-mvc-5 model-binding

我有这两个模型类: -

public partial class SkillLevel
{
  public int SkillID { get; set; }
  public int LevelID { get; set; }
  public string Description { get; set; }
  public string TestProcess { get; set; }

  public virtual Level Level { get; set; }
  public virtual Skill Skill { get; set; }
}

public partial class Skill
{
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
  public Skill()
  {
    this.SkillLevels = new HashSet<SkillLevel>();
    this.SkillLevelStaffs = new HashSet<SkillLevelStaff>();
    this.SkillVersionHistories = new HashSet<SkillVersionHistory>();
    this.Customers = new HashSet<Customer>();
    this.LinkToKBs = new HashSet<LinkToKB>();
  }

  public int SkillID { get; set; }
  public string Name { get; set; }
  public bool IsAllCustomer { get; set; }
  public string Description { get; set; }
  //code goes here

  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
  public virtual ICollection<SkillLevel> SkillLevels { get; set; }

}

现在每项技能都有0到4个SkillLevel条记录,我需要修改“技能”创建视图以显示四个部分,用户可以在其中添加skilllevel.Descriptionskilllevel.TestProcess。我知道我可以使用[i]绑定一个集合,但不知道我需要如何修改我的EditorFor来处理这个问题,所以要在Skill Create视图中包含一些内容:

@model SkillManagement.Models.Skill
<h2>Create</h2>
@using (Html.BeginForm()) 
{
  @Html.AntiForgeryToken()
  <div class="form-horizontal">
    <h4>Skill</h4>
    <hr />
    @Html.ValidationSummary(true)
    **//TESTING ONLY
    @Html.EditorFor(model => model.SkillLevels.[0].TestProcess)
    @Html.EditorFor(model => model.SkillLevels.[0].Description)
    <br/>
    @Html.EditorFor(model => model.SkillLevels.[1].TestProcess)
    @Html.EditorFor(model => model.SkillLevels.[1].Description)**
    <div class="form-group">
      <div class="col-md-offset-2 col-md-10">
        <input type="submit" value="Create" class="btn btn-default" />
      </div>
    </div>
  </div>
}
<div>
  @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
  @Scripts.Render("~/bundles/jqueryval")
}

后期创建操作方法是:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create( Skill skill)
{
  if (ModelState.IsValid)
  {

有人可以这样做吗?

修改

我在创建视图中写了以下内容: -

@for (var i = 0; i < 4; i++)
{  
  <div>
    @Html.Editor("SkillLevels[" + i.ToString() + "].TestProcess") 
    @Html.Editor("SkillLevels[" + i.ToString() + "].Description") 
    @{string v = (i + 1).ToString();}
    @Html.Hidden("SkillLevels["+i.ToString()+"].LevelID", v  ) 
  </div>
}

我有以下Post Create Action方法:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create( Skill skill)
{
  if (ModelState.IsValid)
  {
    db.Skills.Add(skill);        
    await db.SaveChangesAsync();
    return RedirectToAction("Index");
  }

现在我能够添加技能+ 4个技能级别,这很棒。

但如果我对Edit使用相同的视图,那么

....     
@Html.Editor("SkillLevels[" + i.ToString() + "].TestProcess") 
@Html.Editor("SkillLevels[" + i.ToString() + "].Description") 
....

将渲染空字段,而不是我,但它会读取模型属性并显示4个技能级别,以便用户可以编辑它们。你能告诉我如何构建我的编辑视图吗? 感谢

0 个答案:

没有答案