ASP.NET MVC:有所有的景色之一模型 [英] ASP.NET MVC: having one model for all views

查看:99
本文介绍了ASP.NET MVC:有所有的景色之一模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢模型在Django绑定方式运行:您发送您在一个JSON样的方式需要的东西:

 参数1:VAL,
'参数2':VAL

在ASP.NET MVC你不能主要是因为C#是一种静态语言做到这一点。您的可以的使用的ViewData ,但它的丑陋,不建议使用。

因此​​,我不得不为每个视图中创建数十个的ViewModels的:IndexViewModel,AboutViewModel等今天我有一个想法:为什么不创建一个模式类,并有Visual Studio中生成所有必要的字段?这几乎就像在Django的。

 返回查看(新型号
            {
                参数1 =ASD
                参数2 =自卫队
            });

&参数1安培;参数2不是模型类的成员,而Visual Studio将自动生成它们。

现在我的问题是,是不是要去工作?问题是,会有很多在这个Model类领域。当我们把它传递给一个图,将仅用于特别是鉴于制成领域的一小部分。难道会是糟糕的表现明智?

感谢


解决方案

如果您对您的视图模型所有这些属性时未使用(即空或其他)这是不是真的要影响性能。什么会影响却是你的设计。

一般而言,一种模式来统治他们,你提议是有点邪恶,违背了关注点分离。视图模型的是非常简单和进行调整的观点。视图模型的不确实应该提供具有比该视图以呈现需要更多或更少的数据的图。

考虑这个....

您有与它15性能的通用模型,你只把其中一把。别人设计一个新的观点,并期待在模型中,他们可能不知道这些属性的发送,并在它们被设置什么条件。因此它们可以试图显示不存在的数据。这不是一个很干净的做法。

我会坚持个人看法模型,那里有观点之间的通用功能,创建一个抽象或基视图模型从其他视图模型可以延长。

编辑::您可以做的另一件事是使用新的MVC 3(仍然是preVIEW)语法(动态)直接设置ViewData的性质,如果他们的属性。

因此​​,而不是做

 计算机[名字] =鲍勃;

您可以做

  ViewModel.FirstName =鲍勃;

这为您提供了动态变量自动MVC 3。

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

解决方案

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.

这篇关于ASP.NET MVC:有所有的景色之一模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆