2016-04-25 37 views
-1

我有一个包含Milestone列表的模型,我希望该列表可以在网页中使用给定的一组文本框填充(模型内部)。如何在模型中填充数组以便回发

public class Project 
{ 
    public string ProjectNumber { get; set; } 
    public IList<Parameter> Parameters; 
    public IList<Milestone> MilestoneList = new List<Milestone>(); 
} 

而在我的Html.BeginForm("Edit", "Home", FormMethod.Post, new { Project = Model }))里面我有下面的TextBox。

@for (int i = 0; i < Model.MilestoneList.Count; i++) 
{ 
    <td style="align-content: center;">@Html.TextBoxFor(model=>model.MilestoneList[i].Value)</td> 
} 

我在下面milestonelist我的控制问题一直是样板工程null

[HttpPost] 
public ActionResult Edit(Project project) 
{ 
    helper.CreateProject(project, System.Web.HttpContext.Current.User.Identity.Name); 
    return View(); 
} 

所以,我应该怎么这么编程模型内部名单将通过文本框填充?

回答

2

您在模型中使用的字段,而不是属性,所以DefaultModelBinder不能设置的值。将您的型号更改为

public class Project 
{ 
    public string ProjectNumber { get; set; } 
    public IList<Parameter> Parameters { get; set; } 
    public IList<Milestone> MilestoneList { get; set; } 
} 

并初始化控制器中的集合。