自定义对象的模型绑定IList

发布于 2024-10-08 07:02:04 字数 2564 浏览 2 评论 0原文

我有一个 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 类的表单。我已在此处阅读:

ASP.NET MVC 模型绑定 IList<>

模型绑定应该能够填充列表(例如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆悲凉 2024-10-15 07:02:04

请随意投票支持删除。解决办法是用FilterList替换items。就是这样。

基督教

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

Christian

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文