自定义对象的模型绑定IList
我有一个 A 类,其中包含:
public IList<PropertyValueOperators> FilterList { get; set; }
其中 PropertyValueOperators:
public class PropertyValueOperators
{
public string Property { get; set; }
public string Value { get; set; }
public string LikeOperator { get; set; }
}
我还有一个强类型视图,它创建基于 A 类的表单。我已在此处阅读:
模型绑定应该能够填充列表(例如 FilterList)的参数
<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">
,因此我实现了一个 HTML 帮助器,它生成如下内容:不幸的是,当我发布此表单时,FilterList 的计数为 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请随意投票支持删除。解决办法是用FilterList替换items。就是这样。
基督教
Please feel free to vote for deletion. The solution is to replace items with FilterList. That's it.
Christian