如果我总是用一个视图模型还是确定使用的ViewData? [英] Should I always use a view model or is it ok to use ViewData?
问题描述
当是它更好地在一个视图模型使用的ViewData?
when do you think is it better to use ViewData over a view model?
我在几个主要观点完全相同的局部视图。我想控制的局部视图是如何渲染,但我也想preFER局部视图只接受这是一个记录的集合视图模型,只是一个纯粹的IEnumerable<>
对象。我宁愿避免从主视图发送完整视图模型对象,因为它也包含了很多不同的属性,对象,控制分页,排序,过滤等。
I have the same exact partial view in a couple of main views. I'd like to control how a partial view is rendered but I'd also prefer the partial view to accept only a view model which is a collection of records, just a pure IEnumerable<>
object. I'd rather avoid to send the full view model object from a main view because it also contains a lot of different properties, objects, that control paging, sorting, filtering etc.
因此,问题是,如果我要创建另一个视图模型的局部视图还是确定使用的ViewData?我读过soemwhere,使用的ViewData code>是一个非常不好的做法。
Therefore the question is if I should create another view model for the partial view or is it ok to use ViewData? I've read soemwhere that using ViewData
is a very bad practice.
通过查看数据,我可以简单地传递需要这样的细节:
With View Data, I could simply pass require details like this:
@{
ViewDataDictionary vd = new ViewDataDictionary
{
new KeyValuePair<string,object>("WithDelete", Model.WithDelete),
new KeyValuePair<string,object>("WithRadarCode", Model.WithCode)
};
}
// ...
@if (Model != null) {
Html.RenderPartial("_ListOfRecordingsToRender", Model.recordingViewModel, vd);
}
目前,将它整理出来。
At the moment, it would be sorted out.
我担心的是,目前这个 *。recordingViewModel
有很多不同的变化在我的项目,因为不同的模型来创建/编辑工作,上市,shoing记录的细节等等。我觉得它可能会开始在我的项目太乱了,如果我做每个动作视图模式。
My worry is that currently this *.recordingViewModel
has plenty of different variations in my project, because of different models for creating / editting, listing, shoing details of a record etc. I feel like it may start to be too messy in my project if I make view model for each action.
你觉得呢。能不能请您指点在那个特定的问题。谢谢
What do you think. Please could you advice on that particular problem. Thanks
推荐答案
我想你应该坚持使用视图模型
,您视图模型
是定义视图您的要求的类。
I think you should stick to using a ViewModel
, your ViewModel
is the class that defines your requirements for the view.
我这背后的原因是,从长远来看,这将是一个很多更容易维护。当使用 ViewBag
这是一个动态
类,以便在你的意见,你应该检查是否 ViewBag
属性存在(并可能导致愚蠢的错误,错字一样的),例如:
My reason behind this is that in the long run, it will be a lot more maintainable. When using ViewBag
it's a dynamic
class so in your views you should be checking if the ViewBag
property exists (And can lead to silly mistakes like typo's) e.g.:
if(ViewBag.PropertyName != null)
{
// ViewBag.PropertyName is a different property to ViewBag.propertyName
}
这类型code可以使您查看的相当混乱。如果使用强类型的模型,你应该能够把大部分的逻辑在控制器和保持查看尽可能干净这是在我的书一个巨大的利好。
This type of code can make your View's quite messy. If you use a strongly typed model, you should be able to put most of the logic in your controllers and keep the View as clean as possible which is a massive plus in my books.
您也也将结束了(如果你使用 ViewBag
)试图维持它在某一点而奋斗。要删除有关C#一个伟大的事情,这是一个强类型语言! ViewBag
不是强类型,你可能会认为你传递一个列表&LT; T&GT;
,但你可能只是路过一个字符串
。
You also will also end up (if you use ViewBag
) attempting to maintain it at some point and struggle. You are removing one great thing about C#, it's a strongly typed language! ViewBag
is not strongly typed, you may think you are passing in a List<T>
but you could just be passing a string
.
最后一点,你也将失去了在Visual Studio中的智能感知功能。
One last point, you also will lose out on any intellisense features in Visual Studio.
我觉得它可能会开始在我的项目太乱了,如果我做每个动作视图模式。
I feel like it may start to be too messy in my project if I make view model for each action.
惯于它只是在你的控制器分配的一切作为凌乱 ViewBag
?如果这是一个视图模型
,你可以把它送上一个映射类的DTO映射到您的视图。
Wont it just be as messy in your controllers assigning everything to a ViewBag
? If it was a ViewModel
you could send it off to a 'Mapping' class to map your DTO to your View.
而不是这样的:
// ViewModel
var model = new CustomViewModel()
{
PropertyOne = result.FirstResult,
PropertyTwo = result.SecondResult,
}
//ViewBag
ViewBag.PropertyOne = result.FirstResult;
ViewBag.PropertyTwo = result.SecondResult;
您可以这样做:
var mapper = new Map();
var model = mapper.MapToViewModel(result);
*您显然需要提供执行力度的映射类,考察一下 Automapper
我想也preFER局部视图只接受这是一个记录的集合,只是一个纯粹的IEnumerable℃的视图模型;>对象。我宁愿避免从主视图发送完整视图模型对象,因为它也包含了很多不同的属性,对象,控制分页,排序,过滤等。
I'd also prefer the partial view to accept only a view model which is a collection of records, just a pure IEnumerable<> object. I'd rather avoid to send the full view model object from a main view because it also contains a lot of different properties, objects, that control paging, sorting, filtering etc.
这是好的,只是创建具有的一个属性LT视图模型的IEnumerable&; T&GT;
。在我看来,你应该尝试,并在所有的情况下使用强类型视图模型
。
That is fine, just create a view model that has a property of IEnumerable<T>
. In my opinion you should try and use a strongly typed ViewModel
in all of your scenarios.
这篇关于如果我总是用一个视图模型还是确定使用的ViewData?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!