如何使用表单集合将IList <string>传递给Controller

时间:2016-11-07 10:40:44

标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor

我正在尝试将我的数据绑定到我的模型上,这是一个IList。以下是我的观点代码:

@using (Html.BeginForm("fncModify", "Main", FormMethod.Post, 
new { enctype = "multipart/form-data" }))

for (int x = 0; x < Model.lstEmployeeNames.Count; x++)
         {
             @Html.HiddenFor(y=>y.lstEmployeeNames[x])
         }

在我的模型视图中:

public IList<string> lstEmployeeNames { get; set; }

但是在我的控制器视图中,我的表单值总是返回一个空值:

 public ActionResult fncModify(FormCollection form)
    {
        IList<string> lstResult = new List<string>();
        //this always return null
        var lstResult = form["lstEmployeeNames"];
        return RedirectToAction("Index", "Main");
    }

您认为我在这里失踪了什么?我打算将Model.lstEmployeeNames中的字符串列表传递给我的控制器。

1 个答案:

答案 0 :(得分:0)

正如用户GSerg建议的那样,我可以通过使用以下代码直接将List映射到我的ActionResult方法,将我的集合从剃刀视图传递到我的控制器:

public ActionResult fncModify(List<string> lstEmployeeNames)

而不是使用它:

public ActionResult fncModify(FormCollection form)