Manjuboyz
Manjuboyz

Reputation: 7056

Preserving the value of ViewData MVC 4

I am working on MVC 4 where i have "ActionResult" method which is of login page, username and password are entered in textbox, based on the username(which is billnumber) after login it should pull the data of that respective billnumber(username), so currently everything is working fine, I am able to get the username from one view and send it to another for further verification, I have stored "username" and "usertypeID" in Viewdata and that value is passed to next view, but the data on ViewData will not stand for longer time, and even if we try to refresh the page multiple times(10 to 20 times), below are the code which i Have used to store and to access

this is where i store the value

   public ActionResult ValidateLogIn(FormCollection postedFormData)
    {
        // codes
  TempData["UsrName"] = LoginViewModel.LoginDataModel.UserName;
            // codes

 }


  public ActionResult LandingPage()
    {           
        ViewData["message"] = TempData["UsrName"].ToString();
        ViewData["person"] =Convert.ToInt32(TempData["UserTypeID"]);
        TempData.Keep();

        PatientDetailsViewModel PatientDetailsViewModel = new PatientDetailsViewModel();
          String PatID = Convert.ToString(ViewData["message"].ToString());
        int PersonType = Convert.ToInt32(ViewData["person"]);
          
             PatientUnderDoctorDetailsViewModel = PatientUnderDoctorDataAccessService.PatientUnderDocLogInEnquiry(PatID);
   }

this is where i store it on viewdata

        ViewData["message"] = TempData["UsrName"].ToString();
        ViewData["person"] =Convert.ToInt32(TempData["UserTypeID"]);
        TempData.Keep();

AND HERE IS WHERE I GET VALUES FROM VIEWDATA

        String PatID = Convert.ToString(ViewData["message"].ToString());
        int PersonType = Convert.ToInt32(ViewData["person"]);

I am passing PatID and PersonType as parameter to next method,

at beginning I wasn't using TempData.keep(); so when I refresh the page atleast once used to get error, tried searching and found TempData but i believe it is not much efficient for longer time, if I left application idle for 5 mins and then refreshes the page once , it generates error message since tempdata is empty(null)

WHAT I NEED

let me know is there any mistake i have done, or is there any better way to fix this issue, where data can be stored in variable until I quit the application

enter image description here

Upvotes: 0

Views: 1473

Answers (2)

KevDevMan
KevDevMan

Reputation: 818

Don't use Temp data for this. it will be lost when the app pool refreshes or if you use another process (web garden).

User.Identity.Name will return the name of a validated user.

If you are passing data around you should be using Models and not ViewData or TempData or ViewBag.

Upvotes: 0

I hope this link will help you State Management In MVC

Upvotes: 0

Related Questions