ViewBag,ViewData的和TempData的 [英] ViewBag, ViewData and TempData

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

问题描述

可以在任何身体解释,何时使用


  1. 的TempData
  2. ViewBag

  3. 的ViewData

我有一个要求,我需要在一个控制器上的一个设定值时,该控制器将重定向到两个控制器和控制器两将渲染视图。

我曾尝试使用ViewBag,价值得到由我伸出两个控制器的时候丢失了。

我能知道什么时候使用,优势和劣势?

感谢


解决方案

  

1)TempData的


允许你存储将为重定向生存数据。在内部它使用会话烘焙店,它只是重定向作出后的数据将被自动驱逐。该模式是以下内容:

 公众的ActionResult美孚()
{
    //商店的东西到TempData的一个重定向期间将可
    TempData的[富] =酒吧;    //如果你存储的东西进入到TempData的你应该总是重定向
    //一个控制器动作,这将消耗该数据
    返回RedirectToAction(巴);
}公众的ActionResult酒吧()
{
    无功富= TempData的[富];
    ...
}


  

2)ViewBag,ViewData的


允许你存储数据将在相应的视图中使用的控制器操作。这是假设的动作返回一个视图,并且不重定向。仅在当前请求的生命。

该模式是这样的:

 公众的ActionResult美孚()
{
    ViewBag.Foo =酒吧;
    返回查看();
}

和视图:

  @ ViewBag.Foo

或ViewData的:

 公众的ActionResult美孚()
{
    计算机[富] =酒吧;
    返回查看();
}

和视图:

  @ViewData [富]

ViewBag 就位于的ViewData 动态包装和只存在于ASP.NET MVC 3。

这是说,应该被使用没有这两个构造。您应该使用视图模型和强类型的意见。所以,正确的模式如下:

查看模式:

 公共类MyViewModel
{
    公共字符串美孚{搞定;组; }
}

动作:

 公益行动美孚()
{
    VAR模型=新MyViewModel {美孚=酒吧};
    返回查看(模型);
}

强类型的视图:

  @model MyViewModel
@ Model.Foo


在这个简短的介绍,让我们回答你的问题:


  

我的要求是我想在控制器的一个设定值,即
  控制器将重定向到ControllerTwo和控制器2将呈现
  查看。


 公共类OneController:控制器
{
    公众的ActionResult指数()
    {
        TempData的[富] =酒吧;
        返回RedirectToAction(指数,二);
    }
}公共类TwoController:控制器
{
    公众的ActionResult指数()
    {
        VAR模型=新MyViewModel
        {
            美孚= TempData的[富]作为字符串
        };
        返回查看(模型);
    }
}

和相应的视图(〜/查看/二/ Index.cshtml

  @model MyViewModel
@ Html.DisplayFor(X => x.Foo)


有使用的TempData以及的缺点:如果用户点击目标页面中的数据将会丢失上的F5

我个人不使用TempData的都不是。这是因为在内部它使用会话和我在应用程序关闭会话。我preFER更RESTful的方式来实现这一目标。那就是:在重定向时执行重定向商店中的对象数据存储和用户生成的唯一ID的第一个控制器动作。然后在目标动作用这个ID来获取回到初始存储的对象:

 公共类OneController:控制器
{
    公众的ActionResult指数()
    {
        VAR ID = Repository.SaveData(富);
        返回RedirectToAction(指数,二,新{ID = ID});
    }
}公共类TwoController:控制器
{
    公众的ActionResult指数(字符串ID)
    {
        VAR模型=新MyViewModel
        {
            美孚= Repository.GetData(ID)
        };
        返回查看(模型);
    }
}

的观点保持不变。

Could any body explain, when to use

  1. TempData
  2. ViewBag
  3. ViewData

I have a requirement, where I need to set a value in a controller one, that controller will redirect to Controller Two and Controller Two will render the View.

I have tried to use ViewBag, the value gets lost by the time I reach Controller Two.

Can I know when to use and advantages or disadvantages?

Thanks

解决方案

1)TempData

Allows you to store data that will survive for a redirect. Internally it uses the Session as baking store, it's just that after the redirect is made the data is automatically evicted. The pattern is the following:

public ActionResult Foo()
{
    // store something into the tempdata that will be available during a single redirect
    TempData["foo"] = "bar";

    // you should always redirect if you store something into TempData to
    // a controller action that will consume this data
    return RedirectToAction("bar");
}

public ActionResult Bar()
{
    var foo = TempData["foo"];
    ...
}

2)ViewBag, ViewData

Allows you to store data in a controller action that will be used in the corresponding view. This assumes that the action returns a view and doesn't redirect. Lives only during the current request.

The pattern is the following:

public ActionResult Foo()
{
    ViewBag.Foo = "bar";
    return View();
}

and in the view:

@ViewBag.Foo

or with ViewData:

public ActionResult Foo()
{
    ViewData["Foo"] = "bar";
    return View();
}

and in the view:

@ViewData["Foo"]

ViewBag is just a dynamic wrapper around ViewData and exists only in ASP.NET MVC 3.

This being said, none of those two constructs should ever be used. You should use view models and strongly typed views. So the correct pattern is the following:

View model:

public class MyViewModel
{
    public string Foo { get; set; }
}

Action:

public Action Foo()
{
    var model = new MyViewModel { Foo = "bar" };
    return View(model);
}

Strongly typed view:

@model MyViewModel
@Model.Foo


After this brief introduction let's answer your question:

My requirement is I want to set a value in a controller one, that controller will redirect to ControllerTwo and Controller2 will render the View.

public class OneController: Controller
{
    public ActionResult Index()
    {
        TempData["foo"] = "bar";
        return RedirectToAction("index", "two");
    }
}

public class TwoController: Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            Foo = TempData["foo"] as string
        };
        return View(model);
    }
}

and the corresponding view (~/Views/Two/Index.cshtml):

@model MyViewModel
@Html.DisplayFor(x => x.Foo)


There are drawbacks of using TempData as well: if the user hits F5 on the target page the data will be lost.

Personally I don't use TempData neither. It's because internally it uses Session and I disable session in my applications. I prefer a more RESTful way to achieve this. Which is: in the first controller action that performs the redirect store the object in your data store and user the generated unique id when redirecting. Then on the target action use this id to fetch back the initially stored object:

public class OneController: Controller
{
    public ActionResult Index()
    {
        var id = Repository.SaveData("foo");
        return RedirectToAction("index", "two", new { id = id });
    }
}

public class TwoController: Controller
{
    public ActionResult Index(string id)
    {
        var model = new MyViewModel
        {
            Foo = Repository.GetData(id)
        };
        return View(model);
    }
}

The view stays the same.

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

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