MVC 视图中有多少服务器端代码
我一直在使用 Razor 开发 MVC 3 应用程序。我想知道在我看来多少代码是可以接受的。在某些情况下,我需要创建、实例化和使用模型中未包含的对象,而使用 ViewModel 会使模型对象变得臃肿。现在,当我查看我的一些视图时,我发现它们充满了服务器端代码。处理此类问题时的最佳做法是什么?
I have been developing MVC 3 applications using Razor. I was wondering how much code would be acceptable in my views. There were situation where I needed to create, instantiate and use an object which wasn't included in the model and using ViewModel would make the model object bloated. Now when I look at some of my views, I find them full of server side codes. What would be the best practice when dealing with such issues?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您所说的代码指的是 C# 代码,那么准确地说应该是 0。另一方面,在视图中编写 HTML 标记并调用 HTML 帮助程序是完全可以的。
然后看起来你的视图模型没有适应这个视图(因为这个视图需要额外的信息)。因此,通过调整视图模型并包含其所需的一切来改变这种情况。
是的,那太可怕了。只需调整您的视图模型并将此代码重构到您的视图模型或控制器中,甚至编写自定义 HTML 帮助程序即可。
If by code you mean C# code then there should be exactly 0 to be precise. On the other hand it is perfectly fine to write HTML markup in your views and call HTML helpers.
Then it seems that your view model was not adapted to this view (since this view requires additional information). So change this situation by adapting the view model to it and including everything that it needs.
Yes, that's horrible. Simply adapt your view models and refactor this code into your view models or controllers or even write custom HTML helpers.