Accepting parameters or raw data in the controller?

I was wondering if it is possible to have a params argument in a controller function or something similar that would allow me to handle the X number of records in my form.

For example, I have a form that contains X "name" elements that are automatically generated through jQuery. An example of these title elements might be the following:

<input type="text" name="studentName1"></input> <input type="text" name="studentName2"></input> <input type="text" name="studentName3"></input> 

Now, each time, different student names are changed, so it’s quite difficult for me to process form data in my controller. I had something like the following two examples, but, of course, they will not work in reality.

 [HttpPost] public ActionResult PostStudentNames(params string[] studentNames) 

Or:

 [HttpPost] public ActionResult PostStudentNames(string[] formValues) 

Is it possible to achieve something like this?

+2
source share
3 answers

I just want to call back with a different approach that you can use to do this. If this is more convenient, you can model the binding directly to collections of primitive or complex types. Here are two examples:

index.cshtml:

 @using (Html.BeginForm("ListStrings", "Home")) { <p>Bind a collection of strings:</p> <input type="text" name="[0]" value="The quick" /><br /> <input type="text" name="[1]" value="brown fox" /><br /> <input type="text" name="[2]" value="jumped over" /><br /> <input type="text" name="[3]" value="the donkey" /><br /> <input type="submit" value="List" /> } @using (Html.BeginForm("ListComplexModel", "Home")) { <p>Bind a collection of complex models:</p> <input type="text" name="[0].Id" value="1" /><br /> <input type="text" name="[0].Name" value="Bob" /><br /> <input type="text" name="[1].Id" value="2" /><br /> <input type="text" name="[1].Name" value="Jane" /><br /> <input type="submit" value="List" /> } 

Student.cs:

 public class Student { public int Id { get; set; } public string Name { get; set; } } 

HomeController.cs:

 public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult ListStrings(List<string> items) { return View(items); } public ActionResult ListComplexModel(List<Student> items) { return View(items); } } 

ListStrings.cshtml:

 @foreach (var item in Model) { <p>@item</p> } 

ListComplexModel.cshtml:

 @foreach (var item in Model) { <p>@item.Id. @item.Name</p> } 

The first form simply links the list of strings. The second, binds the form data to the List<Student> . Using this approach, you can let the default model binding do some tedious work for you.

Updated for comments

Yes, you can do this too:

the form:

 @using (Html.BeginForm("ListComplexModel", "Home")) { <p>Bind a collection of complex models:</p> <input type="text" name="[0].Id" value="1" /><br /> <input type="text" name="[0].Name" value="Bob" /><br /> <input type="text" name="[1].Id" value="2" /><br /> <input type="text" name="[1].Name" value="Jane" /><br /> <input type="text" name="ClassId" value="13" /><br /> <input type="submit" value="List" /> } 

Controller action:

 public ActionResult ListComplexModel(List<Student> items, int ClassId) { // do stuff } 
+4
source

Matthias,

This works fine without resorting to the params object. your management form:

 <input type="text" name="studentName" /> <input type="text" name="studentName" /> <input type="text" name="studentName" /> <input type="text" name="professorName" /> 

You would use a FormCollection object that would contain all your form elements as comma-separated lists (if the control array) or as separate properties. In the above example, this is what we get:

 [HttpPost] public ActionResult PostStudentNames(FormCollection formValues) { // basic check for rogue commas inside input controls // would need far more sophistication in a #real# app :) var valueStudents = formValues["studentName"].Split(',') .Where(x => x.Length > 0).ToArray(); var valueProfessor = formValues["professorName"]; // other stuff } 

etc. At least this is my recollection of this from a recent project. :)

+3
source
 <input type="text" name="studentName[0]"></input> <input type="text" name="studentName[1]"></input> <input type="text" name="studentName[2]"></input> 

 public ActionResult PostStudentNames(string[] studentName) { } 
+1
source

All Articles