Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net MVC 2 - Iterate Through Form Values In Model Binder

I have a list of items in my form which are named like this...

<input type="text" id="ListItem1" name="ListItem1">
<input type="text" id="ListItem2" name="ListItem2">
<input type="text" id="ListItem3" name="ListItem3">

I want to create a custom model binder which converts these in to model with this structure...

public class MyModel
{
  public IEnumerable<MyModelItem> Items {get; set;}
}

public class MyModelItem
{
  public int Id { get; set; }
  public string Value { get; set; }
}

So each ListItem should be converted to a MyModelItem with id equal to the number at the end of the input id and value set to the value on the input field.

In ASP.Net MVC 1.0 I could iterate over the bindingContext.ValueProvider.Keys collection and check for key.StartsWith("ListItem") to find all input items in this format.

The new IValueProvider interface in ASP.Net MVC 2 does not have a keys collection and I cannot iterate over that interface. How can I access these values which I only know the prefix for at design time in ASP.Net MVC 2?

like image 877
Noob Avatar asked Mar 29 '10 10:03

Noob


1 Answers

If you want to iterate over the form values, use Request.Form.

like image 63
Levi Avatar answered Oct 21 '22 01:10

Levi