Asp.Net MVC 2 - Iterate Through Form Values In Model Binder
Posted
by Noob
on Stack Overflow
See other posts from Stack Overflow
or by Noob
Published on 2010-03-29T10:19:24Z
Indexed on
2010/03/29
10:23 UTC
Read the original article
Hit count: 620
asp.net-mvc
|modelbinders
I have a list of items in my form which are named like this...
<input type="text" id="ListItem1">
<input type="text" id="ListItem2">
<input type="text" id="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?
© Stack Overflow or respective owner