MVC 视图中有多少服务器端代码

发布于 2024-12-25 18:39:33 字数 153 浏览 4 评论 0原文

我一直在使用 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 技术交流群。

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

发布评论

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

评论(1

ぃ双果 2025-01-01 18:39:33

我想知道在我看来多少代码是可以接受的。

如果您所说的代码指的是 C# 代码,那么准确地说应该是 0。另一方面,在视图中编写 HTML 标记并调用 HTML 帮助程序是完全可以的。

在某些情况下,我需要创建、实例化和使用
未包含在模型中的对象,使用 ViewModel 会
让模型对象变得臃肿

然后看起来你的视图模型没有适应这个视图(因为这个视图需要额外的信息)。因此,通过调整视图模型并包含其所需的一切来改变这种情况。

现在当我看我的一些视图时,我发现它们充满了服务器端
代码。处理此类问题时的最佳做法是什么?

是的,那太可怕了。只需调整您的视图模型并将此代码重构到您的视图模型或控制器中,甚至编写自定义 HTML 帮助程序即可。

I was wondering how much code would be acceptable in my views.

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.

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

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.

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?

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.

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