什么是ViewData的,ViewBag,会话,TempData的正确的时间 [英] What is the right time for ViewData, ViewBag, Session, TempData

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

问题描述

我在编辑一个项目,我在一个控制器方法和TempData的[]在另一个看到了一个会话[]。有4之间的差异还是仅仅4种方式做同样的事情。

I was editing a project and I saw a Session[""] in one controller method and TempData[""] in another. Is there a difference between the 4 or is it just 4 ways to do the same thing.

推荐答案


  • 的ViewData / ViewBag - 仅在当前请求期间有效。您可以在一个控制器动作设置,并在视图中使用它。然后消失。所不同的是,第一个是一本字典,而第二只是一个动态围绕这本词典包装。都指向相同的数据,但。 ViewBag在ASP.NET MVC 3中引入。

  • ViewData/ViewBag - valid only for the duration of the current request. You set it in a controller action and use it in the view. Then it disappears. The difference is that the first is a dictionary whereas the second is just a dynamic wrapper around this dictionary. Both point to the same data though. ViewBag was introduced in ASP.NET MVC 3.
  • 例如:

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

    和视图里面,你可以使用这个值:

    and inside the view you could use this value:

    <div>@ViewData["foo"]</div>
    

    同样的,ViewBag但它是动态的:

    Same with ViewBag but it is dynamic:

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

    和视图里面,你可以使用这个值:

    and inside the view you could use this value:

    <div>@ViewBag.foo</div>
    

    因此​​,大家可以看到的ViewData / ViewBag只是​​将信息传递给相比,这是使用视图模型的经典和推荐的方式从控制器动作的视图的另一种方式:

    So as you can see ViewData/ViewBag are just an alternative way to pass information to a view from a controller action compared to the classic and recommended way which is using a view model:

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

    和则:

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

    和强类型视图中:

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

    正如你可以看到使用视图模型提供从控制器动作将信息传递给视图一个强类型的方法。

    As you can see using view models provide a strongly typed approach in passing information to a view from a controller action.


    • 的TempData - 它允许一个单一后续请求的时间持续信息。你店内TempData的东西,然后重定向。在目标控制器动作,你重定向你可以检索储存里面TempData的值。

    • TempData - it allows for persisting information for the duration of a single subsequent request. You store something inside TempData and then redirect. In the target controller action to which you redirected you could retrieve the value that was stored inside TempData.

    例如:

    public ActionResult Foo()
    {
        TempData["foo"] = "bar";
        return RedirectToAction("bar");
    }
    
    public ActionResult Bar()
    {
        var value = TempData["foo"] as string;
        // use the value here. If you need to pass it to the view you could
        // use ViewData/ViewBag (I can't believe I said that but I will leave it for the moment)
        return View();
    }
    

    ASP.NET MVC将自动失效被存储在的TempData 一旦你读它的价值。在幕后ASP.NET MVC持续的信息到会话

    ASP.NET MVC will automatically expire the value that was stored in TempData once you read it. Under the covers ASP.NET MVC persists the information into the Session.


    • 会议 - 相同的TempData但它永远不会过期 - 这将是适用于所有的请求,而不是一个单一的重定向

    • Session - same as TempData except that it never expires - it will be valid for all requests, not a single redirect.

    这篇关于什么是ViewData的,ViewBag,会话,TempData的正确的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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