在 ASP.NET MVC2 中绑定 ISet
我试图找出将 ISet
(Iesi.Collection
) 的第一个元素绑定为第一个元素的最佳方法。
所以基本上我只需要使用某种具有索引器的集合(而 ISet
没有),然后我可以编写这样的代码(效果很好):
<%: Html.EditorFor(x => x.Company.PrimaryUsers[0].Email) %>
但是由于 ISet 没有索引器我无法使用它。
那么如何在 MVC2 中绑定 ISet 的第一个元素呢?
谢谢,
德米特里。
I am trying to find out what would be the best way to bind first element of ISet
(Iesi.Collection
) as a first element.
So basically I only have to use some kind of collection that has an indexer (and ISet
doesn't) then I can write code like this (which works perfectly well):
<%: Html.EditorFor(x => x.Company.PrimaryUsers[0].Email) %>
But as the ISet has no indexer I cannot use it.
So how can I then bind the first element of ISet in MVC2?
Thanks,
Dmitriy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,这些强类型助手仅适用于集合的索引器属性。他们实际上会在语法中查找左括号和右括号。
一种可能的解决方法是在视图模型类中添加另一个属性,该属性的类型为
IList
并从原始属性填充。在 getter 中,您只需从原始属性返回一个新列表,而在 setter 中,您将重建原始集合,因为它没有顺序的概念。Unfortunately those strongly typed helpers work only with indexer properties for collections. They would actually look for opening and closing
[
]
brackets in the syntax.A possible workaround would be to add another property in your view model class which would be of type
IList
and would be populated from the original property. In the getter you would simply return a new list from the original property and in the setter you would reconstruct the original set as it does not have the notion of order.您可以通过以下方式实现这一点:
EditorFor() 的这个特定重载表示“我将向您传递一个模型,但使用模型的 htmlFieldName 字符串,而不是尝试从表达式中推导出它。”在这种情况下,您必须手动跟踪i。
You can pull this off via the following:
This particular overload of EditorFor() says "I'm going to pass you a model, but use the htmlFieldName string for the model rather than trying to deduce it from the expression." You have to keep track of the i manually in this case.