大量使用ViewBag的 [英] Heavy use of ViewBag

查看:156
本文介绍了大量使用ViewBag的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我大量使用ViewBag在我的MVC应用程序,这被认为是不好的做法?我不确定是否花时间创建的ViewModels(但是我认为这是更适合MVVM,而不是MVC)或继续大量使用的ViewBag。什么是支持和反对这一论据?示例控制器方法将返回它的模型(通常是一个简单的域实体),以及下面的调用到ViewBag:

  ViewBag.TotalItems = data.Count();
        ViewBag.FilteredItems = gridFilters;
        ViewBag.Action =事件;
        ViewBag.Area =人物;
        ViewBag.EntityID = person.EntityID;
        ViewBag.OrganisationID = ID;
        ViewBag.Name =的String.Format({0} {1},person.FirstName,person.LastName);
        ViewBag.IsEnabled = person.IsEnabled;
        ViewBag.EntityID = person.EntityID;
        ViewBag.Favourited = users.IsOnUserFavourites(person.EntityID);        ViewBag.Columns = userColumns;        ViewBag.Title =的人:+的String.Format({0} {1},person.FirstName,person.LastName)+ - 事件;


解决方案

诸如此类的问题,通常会从两侧护栏的答案。很多人觉得利用ViewBag这样是一个不好的设计(包括我自己)。它使你的控制器较少测试。您的意见没有强类型的,等等。

通常是使用视图模型一个很好的做法。而不是有你的模型是一个域模型,建立一个模型,是特定于您所显示的视图。这样,就可以100%定制适合您的需要为这个特定视图。你会发现,你并不真正需要太多,一旦你这样做是为了使用ViewBag。有时可以创造很多额外的code(每个视图一个视图模型),但code为pretty简单,在更改一个视图不会打破任何人。

I make heavy use of the ViewBag in my MVC application, is this considered bad practice? I'm unsure as to whether to spend time creating ViewModels (however I thought that was more suited to MVVM rather than MVC) or continue using the ViewBag heavily. What are the arguments for and against this? Example controller method would return it's model (generally a simple domain entity) as well as the following calls to the ViewBag:

            ViewBag.TotalItems = data.Count();
        ViewBag.FilteredItems = gridFilters;
        ViewBag.Action = "Events";
        ViewBag.Area = "People";
        ViewBag.EntityID = person.EntityID;
        ViewBag.OrganisationID = ID;
        ViewBag.Name = string.Format("{0} {1}", person.FirstName, person.LastName);
        ViewBag.IsEnabled = person.IsEnabled;
        ViewBag.EntityID = person.EntityID;
        ViewBag.Favourited = users.IsOnUserFavourites(person.EntityID);

        ViewBag.Columns = userColumns;

        ViewBag.Title = "Person : " + string.Format("{0} {1}", person.FirstName, person.LastName) + " - Events";

解决方案

Questions like these will usually get answers from both sides of the fence. Many people feel that using ViewBag like this is a bad design (myself included). It makes your controllers less testable. Your views are not strongly typed, etc.

It is usually a good practice to use a ViewModel. Instead of have your model be a domain model, create a model that is specific to the view you are displaying. That way it can be 100% custom tailored to what you need for this specific view. You will find that you don't really need to use ViewBag much once you do this. It can sometimes create a lot of extra code (one view model per view) but the code is pretty simple and making a change to one view will not break any others.

这篇关于大量使用ViewBag的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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