将 FormView 中的列表绑定到 ASP.net Web 表单中的模型
对于大型填写表单,我使用 asp.net FormView 将神奇的数据绑定到我的模型。在这个模型中,我有一个名为 Rides 的属性(如下所示),它公开了一个列表,我显然不想完全替换它。所以我把它设为只读。
但是,这样我就不能再使用数据绑定功能了。这个问题有通用的解决方案吗?
public IList<Ride> Rides
{
get
{
if (this._rides == null)
{
this._rides = new List<Ride>();
}
return this._rides;
}
}
For a large fillin form I use the asp.net FormView for the magic databinding to my model. In this model I've a property called Rides (shown below), which exposes a list, which I obviously not want to be replaced entirely. So I made it readonly.
However, this way I can't use the databinding features anymore. Is there a common solution for this problem?
public IList<Ride> Rides
{
get
{
if (this._rides == null)
{
this._rides = new List<Ride>();
}
return this._rides;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您仍然可以使用数据绑定,而不是 <%# Bind("PropertyName")%>使用 <%# Eval("PropertyName")%>,
因为 Bind 通常用于 2 路绑定 & Eval 通常用于评估底层数据源,当您也有 setter 时,Bind 会很方便。
希望这能提供足够的启发和帮助你会发现它很有帮助。
You can still use the databinding, instead of <%# Bind("PropertyName")%> use <%# Eval("PropertyName")%>,
as Bind is usually for 2 way binding & Eval is usually for evaluation of the underlying datasources, Bind will be handy when you have a setter as well.
Hope this sheds enough light & you will find it helpful.
蒙蒂,
看一下名为 BindingList 的类。绑定列表支持双向绑定。您可以在代码中从您的集合创建它,并将其绑定到 FormView 的数据源属性。我想这就是你想要的。
另外,通过公开 IList,您还没有将其设为只读。我可以重新投射到列表,并且可以一路修改您的项目。如果您确实想将游乐设施公开为只读返回 IEnumerable 并且不返回 ReadOnlyCollection 的列表...将列表重新转换为其他类不会有帮助。
Monty,
Take a look at a class named BindingList. Binding list enables two-way binding. You can create it from Yor collection in code and bidn it to the datasource property of the FormView. I think this is what You want.
Also by exposing IList YOU have not made this thing read-only. I can recast to the List and I can modify You items all the way. If You really want to expose rides as read-only return IEnumerable and return not a List by a ReadOnlyCollection... recasting list to the other class wont' help.
在黑暗中狂野刺伤,不知道这是否可行,但我想你可以尝试...
将 setter(set)放回属性中,但使其分配给自身怎么样?
例如。
这意味着该值是无法从属性中触及的,如果尝试进行设置操作,它不会造成任何损害,并且仍然应该绑定......
Wild stab in the dark, don't know if this will work but I guess you can try...
How about putting the setter (set) back in the property, but make it assign to itself?
eg.
This would mean the value is untouchable from the property, if a set operation is attempted it won't do any damage and should still bind...