ASP.NET MVC:所有视图都有一个模型

发布于 2024-09-20 00:33:30 字数 653 浏览 6 评论 0原文

我喜欢 django 中模型绑定的工作方式:您以类似 Json 的方式发送所需的内容:

'param1': val,
'param2': val

在 ASP.NET MVC 中,您不能这样做,主要是因为 C# 是一种静态语言。您可以使用ViewData,但它很丑陋,不推荐。

因此,我必须为每个视图创建数十个 ViewModel:IndexViewModel、AboutViewModel 等。今天我有了一个想法:为什么不只创建一个 Model 类并让 Visual Studio 生成所有必需的字段呢?这几乎就像在 django 中一样。

return View(new Model
            {
                Param1 = "asd",
                Param2 = "sdf"
            });

参数1 & Param2 不是 Model 类的成员,但 Visual Studio 会自动生成它们。

现在我的问题是,它会起作用吗?问题是这个 Model 类中有很多字段。当我们将其传递给视图时,只会使用专门为该视图创建的一小部分字段。性能会很差吗?

谢谢

I like the way model binding in django works: you send stuff that you need in a Json-like way:

'param1': val,
'param2': val

In ASP.NET MVC you can't do it primarily because C# is a static language. You can use ViewData but it's ugly and not recommended.

So I had to create dozens of ViewModels for every view: IndexViewModel, AboutViewModel, etc. And today I got an idea: why not just create one Model class and have Visual Studio generate all necessary fields? It's almost like in django.

return View(new Model
            {
                Param1 = "asd",
                Param2 = "sdf"
            });

Param1 & Param2 are not members of the Model class, but Visual Studio will automatically generate them.

Now my question is, is it gonna work? The thing is that there will be a lot of fields in this Model class. And when we pass it to a view, only a small percentage of fields made for that view in particular will be used. Is it gonna be bad performance wise?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

九厘米的零° 2024-09-27 00:33:30

如果您的 ViewModel 上有所有这些未使用的属性(即 null 或其他),那么它实际上不会影响性能。然而,它将影响的是你的设计。

一般来说,按照您提议的一种模型来统治所有这些模型有点邪恶,并且违背了关注点分离。 ViewModel 的应该非常简单,并且应该针对视图进行定制。 ViewModel 不应该真正为视图提供比视图渲染所需的更多或更少的数据。

考虑一下......

您有一个包含 15 个属性的通用模型,而您只设置了其中的一小部分。其他人设计新视图并查看模型,他们可能不知道发送哪些属性以及在什么条件下设置它们。因此,他们可能会尝试显示不存在的数据。这不是一个非常干净的方法。

我会坚持使用单独的视图模型,并且在视图之间存在通用功能的情况下,创建一个抽象或基本 ViewModel,其他视图模型可以从中扩展。

编辑:您可以做的另一件事是使用新的 MVC 3(仍在预览版)语法(动态)直接设置 ViewData 属性,就好像它们是属性一样。

这样做,而不是这样做

ViewData["FirstName"] = "Bob";

因此,您可以

ViewModel.FirstName = "Bob";

。这会在 MVC 3 中自动为您提供动态变量。

If you have all these properties on your ViewModel which aren't used (i.e null or whatever) it isn't really going to impact performance. What it will impact however, is your design.

Generally speaking, one model to rule them all as you are proposing is a bit evil and goes against separation of concerns. ViewModel's should be very simple and should be tailored to the view. ViewModel's shouldn't really provide the view with more or less data than what the view needs in order to render.

Consider this....

You have a generic model with 15 properties on it and you only set a handful of them. Somebody else designs a new view and looks at the model, they may not know which of those properties are sent and under what conditions they are set. Consequently they may be attempting to display data that does not exist. This isn't a very clean approach.

I would stick to individual view models and where there is common functionality between views, create an abstraction or base ViewModel from which other view models can extend.

Edit: One other thing you could do is use the new MVC 3 (still in preview) syntax (dynamic) for setting ViewData properties directly as if they were properties.

So rather than doing

ViewData["FirstName"] = "Bob";

You can do

ViewModel.FirstName = "Bob";

This gives you dynamic variables automatically in MVC 3.

硬不硬你别怂 2024-09-27 00:33:30

没有理由它不起作用。

您甚至可以跳过 Model 类并仅使用您需要的成员创建匿名类型。

return View(new { Amount = 108, Message = "Hello" });

使用匿名类型的问题是,您将放弃视图中的自动完成功能,因为您将无法根据模型键入视图。

There's no reason it shouldn't work.

You can even skip the Model class and create an anonymous type with just the members you need.

return View(new { Amount = 108, Message = "Hello" });

The problem using anonymous types is that you're giving up on auto-completion in the view since you won't be able to type the view according to the model.

爱给你人给你 2024-09-27 00:33:30

只需使用动态

Just use dynamic

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