ViewBag, ViewData and TempData

后端 未结 8 1888
半阙折子戏
半阙折子戏 2020-11-22 04:43

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 control

8条回答
  •  故里飘歌
    2020-11-22 05:50

    void Keep()
    
    Calling this method with in the current action ensures that all the items in TempData are not removed at the end of the current request.
    
        @model MyProject.Models.EmpModel;
        @{
        Layout = "~/Views/Shared/_Layout.cshtml";
        ViewBag.Title = "About";
        var tempDataEmployeet = TempData["emp"] as Employee; //need typcasting
        TempData.Keep(); // retains all strings values
        } 
    
    void Keep(string key)
    
    Calling this method with in the current action ensures that specific item in TempData is not removed at the end of the current request.
    
        @model MyProject.Models.EmpModel;
        @{
        Layout = "~/Views/Shared/_Layout.cshtml";
        ViewBag.Title = "About";
        var tempDataEmployeet = TempData["emp"] as Employee; //need typcasting
        TempData.Keep("emp"); // retains only "emp" string values
        } 
    

提交回复
热议问题