简体   繁体   English

我还应该使用什么来在控制器之间传递数据?

[英]What else should I use to pass data between controllers?

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.我正在使用 TempData 在我的控制器之间传递用户 ID 或其他数据,但是当用户按下 ctrl+shift+r 时,我的所有数据都会被清除。 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);

           }
       }

This is the method I usually use to pass data of one controller to another using DependencyResolver这是我通常使用DependencyResolver将一个 controller 的数据传递给另一个的方法

Steps of first controller先controller的步骤

  1. Create public static object of User class inside Home controller (In my case its home controller) like thisHome Z594C103F2C6E04C3D8AB059F031E0C1 内部创建User class 的公共 static object
  2. Inside Index set the userId attributeIndex里面设置userId属性
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第二controller的步数

  1. Inside controller class create an object of first controller and initialize it using DependencyResolver在 controller class 内部创建第一个DependencyResolverobject和 initializeDependeritAZ
  2. Use this object to access the data使用此 object 访问数据
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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM