c# - 如何将列表中的对象传递给另一个控制器索引函数?
问题描述
我是 asp.net-mvc 的新手。我有一个包含少数用户信息的用户列表,在检查用户输入的用户名与列表中的用户名相同后,如何使用 RedirectToAction 方法将匹配的用户传递给另一个控制器?
登录控制器.cs
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Authorise(Models.User x)
{
List<User> userList = new List<User>();
userList.Add(new User { UserName = "A", Password = "A", EmpName ="A", EmpID = "ABC123", EmpEmail = "test@gmail.com", EmpContact = "1300882525" });
userList.Add(new User { UserName = "B", Password = "B", EmpName = "B", EmpID = "ABC123", EmpEmail = "test@gmail.com", EmpContact = "1300882525" });
userList.Add(new User { UserName = "C", Password = "C", EmpName = "C", EmpID = "ABC123", EmpEmail = "test@gmail.com", EmpContact = "1300882525" });
userList.Add(new User { UserName = "E", Password = "E", EmpName = "E", EmpID = "ABC123", EmpEmail = "test@gmail.com", EmpContact = "1300882525" });
var username = x.UserName;
var pass = x.Password;
//String UserName = Request.Form["UserName"];
//String Password = Request.Form["Password"];
if (ModelState.IsValid)
{
if (!userList.Exists(y => y.UserName == username) && !userList.Exists(y => y.Password == pass))
{
TempData["Message"] = "Wrong username or password.";
return RedirectToAction("Index");
}
else if (userList.Exists( y => y.UserName == username) )
{
return RedirectToAction("Index", "User");
}
}
return View("Index");
}
public ActionResult LogOut()
{
Session.Abandon();
return RedirectToAction("Index", "Login");
}
UserController.cs 我如何在这个控制器中传递对象
public ActionResult Index()
{
List<PurchaseHistory> List1 = new List<PurchaseHistory>();
List1.Add(new PurchaseHistory { empName = "A", ItemName = "Speaker", DateTime = new DateTime(2018,12,25) });
List1.Add(new PurchaseHistory { empName = "B", ItemName = "Speaker", DateTime = new DateTime(2018, 12, 25) });
List1.Add(new PurchaseHistory { empName = "C", ItemName = "Speaker", DateTime = new DateTime(2018, 12, 25) });
List1.Add(new PurchaseHistory { empName = "C", ItemName = "Laptop", DateTime = new DateTime(2018, 12, 25) });
List1.Add(new PurchaseHistory { empName = "E", ItemName = "Speaker", DateTime = new DateTime(2018, 12, 25) });
List1.Add(new PurchaseHistory { empName = "E", ItemName = "Speaker", DateTime = new DateTime(2018, 12, 25) });
if (List1.Exists( y => y.empName == x.EmpName))
{
x.ListPurchaseHistory = List1.FindAll(y => y.empName.Equals(x.EmpName));
}
return View(x);
}
解决方案
我看到两个选项:
- 使用临时数据
使用匿名对象并更改操作签名:
RedirectToAction("Index", new { user = your_user }) // ... public ActionResult Index(User user)
推荐阅读
- python - 用 Selenium 提取第一个跨度子项
- php - 无法在 Substr 中提取带有变量的字符串
- apache-spark - 使用 apache-curator 在纱线上的火花流中存储 kafka 偏移量。异常:用户类抛出异常:java.lang.NoSuchMethodError
- django - Django中的数据库配置
- python - 写入新行 - Xlsxwriter
- batch-file - 在 CMD/Batch 中,我可以存储 WMIC 返回的值吗?
- laravel - 将 ckeditor 与 laravel 6 集成
- grouping - 在 Cytoscape.js 中将节点分组在一起
- java - 为什么 Kafka 的 seekToBeginning 和 seekToEnd 不能与 assign 一起使用?
- amazon-s3 - 使用 express.js 将大型视频文件上传到 s3