【问题标题】:How to populate an array in side a model for postback如何在模型中填充数组以进行回发
【发布时间】:2016-04-25 09:11:11
【问题描述】:

我有一个包含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 })) 中,我有以下文本框。

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

我在里程碑列表下方的控制器中的问题始终是模型项目中的null

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

那么我应该如何编程以便模型内的列表将通过 TextBoxes 填充?

【问题讨论】:

    标签: c# asp.net arrays asp.net-mvc model


    【解决方案1】:

    您在模型中使用字段,而不是属性,因此 DefaultModelBinder 无法设置该值。将您的模型更改为

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

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

    【讨论】:

      猜你喜欢
      • 2023-02-17
      • 1970-01-01
      • 2021-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多