开发者

model binding IList of custom object

开发者 https://www.devze.com 2023-01-31 09:23 出处:网络
I have a class A which contains: public IList<PropertyValueOperators> FilterList { get; set; } where PropertyValueOperators:

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&g开发者_如何学编程t;
    </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


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

Christian

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号