【问题标题】:How do I pass a list from a view to a controller?如何将列表从视图传递到控制器?
【发布时间】:2015-04-17 14:18:56
【问题描述】:

我有一个视图,在里面我正在生成一个列表,但是如果字段为空,我无法成功地将它传递给控制器​​:

我认为的代码:

@using (Html.BeginForm("AddFOP", "Revenue", null,  FormMethod.Post, new { enctype = "multipart/form-data" }))
           {
              <table class="grid" style="margin-top: 15px">
                    <thead>
                        <tr>
                            <th>FOP</th>
                            <th>ORGN</th>
                            <th>PROGRAM</th>
                            <th>PERCENTAGE</th>
                            <th></th>

                        </tr>
                    </thead>
                    <tbody>               
                        @for (int i = 0; i < Model.editBillingFOPList.Count;i++ )
                        { 

                         <tr>
                            <td>@Html.TextBox("FOPList[" + @i + "].Fund", 
                                                Model.editBillingFOPList[i].Fund)</td>
                            <td>@Html.TextBox("FOPList[" + @i + "].ORGN", 
                                                Model.editBillingFOPList[i].ORGN)</td>
                            <td>@Html.TextBox("FOPList[" + @i + "].Program", 
                                                Model.editBillingFOPList[i].Program)</td>
                            <td>@Html.TextBox("FOPList[" + @i + "].Percentage", 
                                                Model.editBillingFOPList[i].Percentage)</td>
                        </tr>

这是控制器:

[HttpPost]
        public ActionResult AddFOP(List<BillingFOPModel> FOPList)
        {


            return View();
        }

我的 FOPList 显示计数为 1,如果我在调试模式下展开它,它会显示模型列表但具有空值 - 有什么想法吗?

我的模特:

 public class BillingFOPModel
    {
        public string BillingArea;
        public string BillingAreaName;
        public string Fund;
        public string ORGN;
        public string Program;
        public string Percentage;
    }

这是我的 ViewModel:

  public class EditBillingViewModel
        {   //declare attributes

            public List<BillingFOPModel> editBillingFOPList { get; set; }
            public string newBillingArea { get; set; }
            public string newBillingAreaName { get; set; }
            public string newFund { get; set; }
            public string newORGN { get; set; }
            public string newProgram { get; set; }
            public string newPercentage { get; set; }

            public EditBillingViewModel()
            {

            }

            //constructor that passes a list and 2 strings
            public EditBillingViewModel(List<BillingFOPModel> editBillingFOPList, string newBillingArea, string newBillingAreaName)
            {
                this.editBillingFOPList = editBillingFOPList;
                this.newBillingArea = newBillingArea;
                this.newBillingAreaName = newBillingAreaName;
            }

【问题讨论】:

  • 你为什么不像this question那样使用TextBoxFor?你这样做非常困难
  • 显示你的模型类BillingFOPModel
  • @Jonesy 我不确定我是否可以通过不迭代来实现我所需要的
  • @EhsanSajjad 完成了模型和视图模型
  • @Jonesy 好的,我明白你的意思 - 虽然由于时间限制,这种方式似乎更简单,但我想我会这样做,因为它现在似乎正在工作,但非常感谢你的光芒关于此事!

标签: c# asp.net-mvc model-view-controller


【解决方案1】:

您正在犯与 mvc 的新开发人员最常犯的相同的错误,您的模型具有字段而不是属性,所以这就是您没有发布数据的原因。

将字段更改为模型类中的属性:

public class BillingFOPModel
{
    public string BillingArea {get; set; }
    public string BillingAreaName { get; set;}
    public string Fund { get; set;}
    public string ORGN { get; set;}
    public string Program {get; set;}
    public string Percentage {get; set;}
}

旁注:

尽可能始终使用强类型助手,例如 TextBoxFor()DropDownListFor()

【讨论】:

    【解决方案2】:

    看看这个:http://www.codeguru.com/csharp/.net/net_asp/mvc/using-display-templates-and-editor-templates-in-asp.net-mvc.htm

    使用 EditorTemplates 意味着您不需要在标记中使用 for 循环,因此不必担心在控件上设置正确的名称和 ID。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多