0

I have a class A which contains:

public IList<PropertyValueOperators> FilterList { get; set; }

where PropertyValueOperators:

public class PropertyValueOperators
    {
        public string Property { get; set; }
        public string Value { get; set; }
        public string LikeOperator { get; set; }
    }

I also have a strongly typed view which creates a form based on class A. I have read here:

ASP.NET MVC model binding an IList<> parameter

that the model binding should be able to populate lists such as FilterList so I have implemented an HTML helper which generates something like this:

<label for="items[0].Property">Filter By</label>    
<select id="items[0]_Property" name="items[0].Property">
    <option selected="selected" value="Item.Id">DBId</option>
    <option value="Category_ItemName.Name">Name</option>
    </select>
    <label for="items[0].LikeOperator">Filter Operator</label>
    <select id="items[0]_LikeOperator" name="items[0].LikeOperator">
    <option value="Contains">Contains</option>
    <option value="EndsWith">Ends With</option>
    <option selected="selected" value="Equals">Equals</option>
    <option value="Starts With">Starts With</option>
    </select>
    <label for="items[0].Value">Filter Value</label>
    <input name="items[0].Value" style="width: 100px;" value="920058" id="items[0]_Value" width="5" type="text">
    <br>
    <label for="items[1].Property">Filter By</label>
    <select id="items[1]_Property" name="items[1].Property">
    <option value="Item.Id">DBId</option>
    <option selected="selected" value="Category_ItemName.Name">Name</option>
    </select>
    <label for="items[1].LikeOperator">Filter Operator</label>
    <select id="items[1]_LikeOperator" name="items[1].LikeOperator">
    <option value="Contains">Contains</option>
    <option value="EndsWith">Ends With</option>
    <option value="Equals">Equals</option>
    <option selected="selected" value="Starts With">Starts With</option>
    </select>
    <label for="items[1].Value">Filter Value</label>
    <input name="items[1].Value" style="width: 100px;" value="" id="items[1]_Value" width="5" type="text">

Unfortunately when I post this form, FilterList has a count of 0. Can you see something wrong? Or is it just impossible to achieve what I want without implementing a custom model binder?

Thanks.

Christian

Community
  • 1
  • 1
cs0815
  • 16,751
  • 45
  • 136
  • 299
  • 1
    Is your controller action taking a parameter signature like this `IList items`? –  Dec 21 '10 at 11:41
  • No It takes an instance of A which contains FilterList. OMG I just noticed that I should rename items to FilterList ... will try this. – cs0815 Dec 21 '10 at 11:47
  • 2
    @DaveParsons just submit an answer 'replace items with FilterList' this works. You got me on the right track thanks! – cs0815 Dec 21 '10 at 11:50
  • Just a note, it helps to post all code used. So in this case, the controller action method! – Paul Dec 21 '10 at 23:16
  • @csetzkorn I'm just glad you managed to get it working! I come across problems like this a lot with MVC. So much 'magic' happens behind the scenes that it is sometimes almost impossible to figure out what is going wrong! –  Dec 22 '10 at 14:49
  • @Paul Thanks for the comment. The controller method takes an instance of class A. Thought that's rather obvious (-: Anyway the problem is solved see comments. ASP.NET MVC's model binding is rather powerful. – cs0815 Dec 22 '10 at 15:16

1 Answers1

1

Please feel free to vote for deletion. The solution is to replace items with FilterList. That's it.

Christian

cs0815
  • 16,751
  • 45
  • 136
  • 299