What else should I use to pass data between controllers?

Viewed 37

I'm using TempData to pass userID or other datas between my controllers but when user pushes ctrl+shift+r all my data gets cleaned. Is there a way to prevent this or what else should I use to pass data?

public ActionResult Index(Table_User user)
        {
            var user1 = repo.Find(x => x.UserMail== user.UserMail);
     
            if (user1 != null)
            {
                if (SecurityAlgorithms.CheckMD5Hash(user.UserPassword, user1.UserPassword))
                {
                    
                    FormsAuthentication.SetAuthCookie(user1.UserName, false);
                    Session["user1"] = user.UserName;
                    var id = user.UserID;
                    User userinfo = new User()
                    {
                        userID= id
                    };
                  
                    TempData["UserData"] = userinfo;

                    return RedirectToAction("Index", "Home", new { area = "" });
                }
                
                else
                {
                    ViewBag.ErrorMessage = "Check your password!";
                    return View("Index", user);
                }
            }


public ActionResult Index()
       {
          
           User userdata= TempData["UserData"] as User;
           if (userdata== null)
           {
               ViewBag.ErrorMessage = "error";
               TempData["Message"] = "Error occurred: ";

               return View();
           }
           else
           {
           var values = db.Table_RentedBooks.Where(a => a.UserID== userdata.UserID).ToList();
           return View(values);

           }
       }
1 Answers

This is the method I usually use to pass data of one controller to another using DependencyResolver

Steps of first controller

  1. Create public static object of User class inside Home controller (In my case its home controller) like this
  2. Inside Index set the userId attribute
public class HomeController
{
    public static User _user;
    public HomeController()
    {
        _user = new User();
    }
    public ActionResult Index(Table_User user)
    {
        var user1 = repo.Find(x => x.UserMail== user.UserMail);
     
        if (user1 != null)
        {
            if (SecurityAlgorithms.CheckMD5Hash(user.UserPassword, user1.UserPassword))
            {
                    
                FormsAuthentication.SetAuthCookie(user1.UserName, false);
                Session["user1"] = user.UserName;
                var id = user.UserID;
                //Here set the id
                _user.userID= id;

                return RedirectToAction("Index", "Home", new { area = "" });
            }
                
            else
            {
                ViewBag.ErrorMessage = "Check your password!";
                return View("Index", user);
            }
        }
    }
}

Steps of second controller

  1. Inside controller class create an object of first controller and initialize it using DependencyResolver
  2. Use this object to access the data
public class SecondController
{
    private readonly HomeController homeController = DependencyResolver.Current.GetService<HomeController>();

    public ActionResult Index()
    {  
        User userdata= homeController._user;
        if (userdata== null)
        {
            ViewBag.ErrorMessage = "error";
            TempData["Message"] = "Error occurred: ";

            return View();
        }
        else
        {
            var values = db.Table_RentedBooks.Where(a => a.UserID== userdata.UserID).ToList();
            return View(values);
        }
    }
}
Related