Asp.Net MVC 2 - Iterating Through Form Values ​​in a Binder Model

I have a list of elements in my form that 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 binder that converts them to a 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; } } 

Thus, each ListItem must be converted to MyModelItem with an identifier equal to the number at the end of the input identifier, and the value set to the value in the input field.

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

The new IValueProvider interface in ASP.Net MVC 2 does not have a collection of keys, and I cannot iterate over this interface. How to access these values ​​that I know only for the prefix during development in ASP.Net MVC 2?

+6
asp.net-mvc
source share
2 answers

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

+5
source share

Can you use the bindingContext.ModelState.Keys collection?

Update Sorry, it must have been a little clearer. I meant, could you please not use the ModelState.Keys collection, check for key.StartsWith ("ListItem"), and if so use this key to get the value from the value provider (using the GetValue method).

-one
source share

All Articles