Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 ViewBag、ViewData和TempData_Asp.net Mvc 3 - Fatal编程技术网

Asp.net mvc 3 ViewBag、ViewData和TempData

Asp.net mvc 3 ViewBag、ViewData和TempData,asp.net-mvc-3,Asp.net Mvc 3,有人能解释一下什么时候使用吗 临时数据 视袋 视图数据 我有一个要求,我需要在控制器1中设置一个值,该控制器将重定向到控制器2,控制器2将渲染视图 我尝试使用ViewBag,当我到达控制器2时,该值丢失 我能知道什么时候使用以及优点或缺点吗 谢谢 1) 临时数据 允许您存储将继续用于重定向的数据。在内部,它将会话用作备份存储,在重定向完成后,数据将自动退出。模式如下: public ActionResult Foo() { // store something into the temp

有人能解释一下什么时候使用吗

  • 临时数据
  • 视袋
  • 视图数据
  • 我有一个要求,我需要在控制器1中设置一个值,该控制器将重定向到控制器2,控制器2将渲染视图

    我尝试使用ViewBag,当我到达控制器2时,该值丢失

    我能知道什么时候使用以及优点或缺点吗

    谢谢

    1) 临时数据

    允许您存储将继续用于重定向的数据。在内部,它将会话用作备份存储,在重定向完成后,数据将自动退出。模式如下:

    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"];
        ...
    }
    
    public ActionResult Foo()
    {
        ViewBag.Foo = "bar";
        return View();
    }
    
    2) ViewBag,ViewData

    允许您将数据存储在将在相应视图中使用的控制器操作中。这假设操作返回一个视图而不重定向。仅在当前请求期间有效

    模式如下:

    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"];
        ...
    }
    
    public ActionResult Foo()
    {
        ViewBag.Foo = "bar";
        return View();
    }
    
    并且认为:

    @ViewBag.Foo
    
    @ViewData["Foo"]
    
    或使用ViewData:

    public ActionResult Foo()
    {
        ViewData["Foo"] = "bar";
        return View();
    }
    
    并且认为:

    @ViewBag.Foo
    
    @ViewData["Foo"]
    
    ViewBag
    只是
    ViewData
    的动态包装,仅存在于ASP.NET MVC 3中

    也就是说,这两种结构都不应该被使用。您应该使用视图模型和强类型视图。因此,正确的模式如下所示:

    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"];
        ...
    }
    
    public ActionResult Foo()
    {
        ViewBag.Foo = "bar";
        return View();
    }
    
    视图模型:

    public class MyViewModel
    {
        public string Foo { get; set; }
    }
    
    行动:

    public Action Foo()
    {
        var model = new MyViewModel { Foo = "bar" };
        return View(model);
    }
    
    强类型视图:

    @model MyViewModel
    @Model.Foo
    

    在简要介绍之后,让我们回答您的问题:

    我的要求是我想在控制器1中设置一个值 控制器将重定向到控制器2,控制器2将呈现 景色

    以及相应的视图(
    ~/Views/Two/Index.cshtml
    ):


    使用TempData也有缺点:如果用户在目标页面上点击F5,数据将丢失

    我个人也不使用TempData。这是因为它在内部使用会话,而我在应用程序中禁用会话。我更喜欢一种更安静的方式来实现这一点。即:在执行重定向的第一个控制器操作中,将对象存储在数据存储中,并在重定向时使用生成的唯一id。然后在目标操作上,使用此id取回最初存储的对象:

    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);
        }
    }
    
    视图保持不变。

    TempData 基本上,它就像一个数据阅读器,一旦读取,数据就会丢失

    示例 如果您注意上面的代码,在读取TempData之前,RedirectToAction对TempData没有影响。因此,一旦读取TempData,值就会丢失

    读取后如何保存临时数据? 检查动作方法测试1和测试2的输出

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";
            TempData["T"] = "T";
            return RedirectToAction("About");
        }
    
        public ActionResult About()
        {
            return RedirectToAction("Test1");
        }
    
        public ActionResult Test1()
        {
            string Str = Convert.ToString(TempData["T"]);
            TempData.Keep(); // Keep TempData
            return RedirectToAction("Test2");
        }
    
        public ActionResult Test2()
        {
            string Str = Convert.ToString(TempData["T"]); //OutPut - T
            return View();
        }
    }
    
    如果您注意上面的代码,在重定向到操作以及读取数据后,数据不会丢失,原因是,我们使用的是
    TempData.Keep()
    。是吗

    通过这种方式,您还可以使它在其他控制器中保持多长时间。 ViewBag/ViewData
    数据将保留到MVC中相应的视图

    视图包、视图数据、临时数据和视图状态

    NET MVC为我们提供了三个选项ViewData、VieBag和TempData,用于将数据从控制器传递到视图和在下一个请求中。ViewData和ViewBag几乎相似,TempData承担额外的责任

    ViewBag和ViewData之间的相似性:

    public ActionResult Index()
    
    {  
        ViewBag.Name = "Arun Prakash";
        return View();    
    }
    
    public ActionResult Index()  
    {
        ViewData["Name"] = "Arun Prakash";
        return View(); 
    }
    
    有助于在从控制器移动到视图时维护数据。曾经 将数据从控制器传递到相应的视图。短命意味着 发生重定向时,值变为null。这是因为他们的目标 是提供控制器和视图之间通信的方式。它是 服务器调用中的通信机制

    视图包和视图数据之间的差异:

    public ActionResult Index()
    
    {  
        ViewBag.Name = "Arun Prakash";
        return View();    
    }
    
    public ActionResult Index()  
    {
        ViewData["Name"] = "Arun Prakash";
        return View(); 
    }
    
    ViewData是从中派生的对象字典 ViewDataDictionary类,并可使用字符串作为键进行访问。视袋 是利用新动态功能的动态特性 在C#4.0中。ViewData要求对复杂的数据类型和类型进行类型转换 检查空值以避免错误。ViewBag不需要 复杂数据类型的类型转换

    视图包和视图数据示例:

    public ActionResult Index()
    
    {  
        ViewBag.Name = "Arun Prakash";
        return View();    
    }
    
    public ActionResult Index()  
    {
        ViewData["Name"] = "Arun Prakash";
        return View(); 
    }
    
    鉴于此,我们称之为:

    @ViewBag.Name   
    @ViewData["Name"]
    
    TempData:

    public ActionResult Index()
    
    {  
        ViewBag.Name = "Arun Prakash";
        return View();    
    }
    
    public ActionResult Index()  
    {
        ViewData["Name"] = "Arun Prakash";
        return View(); 
    }
    
    有助于在从一个控制器移动到另一个控制器时维护数据 控制器,或从一个动作切换到另一个动作。换句话说,当你 重定向,“Tempdata”有助于在这些重定向之间维护数据。 它在内部使用会话变量。TempData是一个非常有用的工具 短期实例,您应该仅在当前 以及随后的请求

    使用TempData可靠工作的唯一场景是重定向时。这是因为重定向会终止当前请求(并将HTTP状态代码302对象发送到客户端),然后在服务器上创建一个新请求以服务重定向视图

    它需要对复杂数据类型进行类型转换,并检查空值以避免错误

    public ActionResult Index()
    {   
       var model = new Review()  
       {  
          Body = "Start",  
          Rating=5  
       };  
    
        TempData["ModelName"] = model;    
        return RedirectToAction("About");   
    } 
    
    public ActionResult About()       
    {  
        var model= TempData["ModelName"];  
        return View(model);   
    }  
    
    临时数据 将始终可用,直到第一次阅读,一旦你阅读它不可用的任何更多的可能是有用的传递快速消息,也可以查看将在第一次阅读后消失。 视袋 它在将一段数据快速传递给视图时更有用,通常您应该通过模型将所有数据传递给视图,但在某些情况下,您的模型直接来自映射到类似数据库的实体框架中的类 在这种情况下,您不需要更改模型以传递新数据段,您可以将其粘贴到viewbag中
    ViewData只是ViewBag的索引版本,在MVC3之前使用过,而且ViewBag和ViewData的范围不同。viewbag基于第一个视图(不在操作方法之间共享),但数据可以在操作方法之间共享,也可以仅在另一个操作方法之间共享。

    是一篇解释差异的好文章。stackover