Willy David Jr Willy David Jr - 8 months ago 56
ASP.NET (C#) Question

How to Pass an IList<string> to Controller using Form Collection

I am trying to bind my data on my model which is an IList. Here is my code on my view:

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

for (int x = 0; x < Model.lstEmployeeNames.Count; x++)

On my model view:

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

But on my controller view, my form values always return a null value:

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

What do you think I am missing here? I am planning to pass the list of strings from my Model.lstEmployeeNames to my controller.

Answer Source

As suggested by user GSerg, I was able to pass my collection from my razor view into my controller by directly mapping the List to my ActionResult method using this code:

public ActionResult fncModify(List<string> lstEmployeeNames)

Instead of using this:

public ActionResult fncModify(FormCollection form)