asp.net mvc keep object alive, information

前端 未结 4 365
梦如初夏
梦如初夏 2020-11-30 13:17

i have this code

[HttpPost]
public ActionResult Index(LoginModel loginModel)
{
    if (ModelState.IsValid)
    { 
       // some lines of code . bla bla bla
         


        
4条回答
  •  猫巷女王i
    2020-11-30 14:06

    Confusion

    but when i refresh, everything messes up, it says that the loginModel is like null

    Answer

    This is due to the fact that you have read the TempData key and Once it is read, data will be lost for that particular key.

    var Value = TempData["keyName"] //Once read, data will be lost
    

    Question

    how can i like keep track of the current login users

    Answer

    So to persist the data even after the data is read you can Alive it like below

    var Value = TempData["keyName"];
    TempData.Keep();                 //Data will not be lost for all Keys
    TempData.Keep("keyName");        //Data will not be lost for this Key
    

    TempData works in new Tabs/Windows also, like Session variable does.

    You could use Session Variable also, Only major problem is that Session Variable are very heavy comparing with TempData. Finally you are able to keep the data across Controllers/Area also.

    Hope this post will help you alot.

提交回复
热议问题