在asp.net MVC视图中使用对象方法
一两个月前,当我开始做 ASP.NET MVC 工作时,我记得读过,不建议在视图中调用如下所示的对象方法:
<%: Html.ListBoxFor( m => Model.SelectedId, Model.SelectItems.ToSelectList() ) %>
上面只是一个示例,而不是我正在使用的直接代码行。我正在使用扩展方法将我的枚举转换为 ListBox 绑定就绪的 IEnumerable 集合。我知道有人在一篇关于此的文章中提出了一个担忧,但我不记得它是什么。目前我实际上并没有编码,但我这样做的方式正在成为一种负担。我正在编写扩展方法,将所有 IEnumerable 对象转换为 SelectListItem 集合,现在转换为 MultiSelectList 对象。因此,我的视图模型因显示数据的“潜在”方式而变得臃肿。我跟踪的不仅仅是可用选项的枚举,而是跟踪 4 个新数据:SelectListItem 集合、SelectListValue 对象、MultiSelectList 控件、MultiSelectListValues 集合。
我不反对继续这样做,但我想记住为什么如果我使用强类型的完整/部分视图,建议不要在视图上调用扩展方法。我试图找到那篇文章和其他类似的文章,但找不到。我希望我的记忆力比这更好,但希望有人知道这一点并愿意与我分享!
提前致谢!
A month or two ago when I started doing asp.net MVC work I remember reading that it is not recommended to call object methods like the following in the view:
<%: Html.ListBoxFor( m => Model.SelectedId, Model.SelectItems.ToSelectList() ) %>
That above is just an example, not a direct line of code I am using. I am using extension methods to turn my enumerables into ListBox binding-ready IEnumerable collections. I know there was a concern that someone had raised in an article about this, but I cannot remember what it was. I am not actually coding it this was at the moment, but the way I am doing it is becoming a burden. I am writing extension methods to convert all of my IEnumerable objects into both SelectListItem collections and now a MultiSelectList object. So my view models are becoming bloated with "potential" ways of displaying the data. Instead of their just being an enumerable of available options, I am tracking 4 new pieces of data: SelectListItem collection, SelectListValue object, MultiSelectList control, MultiSelectListValues collection.
I am not opposed to keep going about this, but I would like to remember why it was recommend not to call the extension method on the View if I'm using strongly-typed full/partial views. I tried to find that article and others like it but I could not. I wish my memory was better than this, but hopefully someone out there knows this offhand and would be willing to share it with me!
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所谈论的想法基于这样一个原则:MVC 中的视图应该尽可能简单。因为这是最难测试的,应该避免不必要的膨胀。
您的示例行可以通过几种不同的方式进行配对。您可以尝试编写一个扩展,从模型中获取枚举并将其转换为可以输入列表框的选择列表,或者您可以更改模型的属性以返回列表项的枚举。后者是我的首选方法。模型的存在是为了组织视图要使用的数据,因此没有理由让它输出视图不能直接使用的数据。这些方法中的任何一种都会消除视图中的复杂性,并将其放置在更容易进行单元测试的地方。
希望这有帮助。
The idea your talking about is based on a principal that the view in MVC should be as simple as possible. Because it is the most difficult to test unnecessary bloat should be avoided.
Your example line could be paired down a few different ways. You could try writing an extension that takes the enumeration from your model and converts it to a select list that can be fed into a listbox, or you can change the model's property to return an enumeration of list items. The later is my preferred method. The model exists to organize the data to be consumed by the view so theres no reason to have it output data that cannot be consumed by the view directly. Either one of these methods would take complexity out of the view and place it somewhere that is easier to unit test.
Hope this helps.