[HttpPost]
public ActionResult Create(EmployeeEntry employeeentry)
{
if (ModelState.IsValid)
{
EmployeeEntry employeesales = new EmployeeEntry();
employeesales.stock_id = employeeentry.stock_id;
employeesales.quantity = employeeentry.quantity;
employeesales.total = employeeentry.total;
employeesales.employeeid = (int)TempData["Name"];
db.EmployeeEntries.Add(employeesales);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.stock_id = new SelectList(db.AddProduct, "stock_id", "Product_name", employeeentry.stock_id);
return View(employeeentry);
}
这是我的代码,将 TempData 转换为整数时出现错误。TempData["Name"] 包含已登录员工的 ID。
var Result = from a in db.login where a.userName == login.userName && a.Password == login.Password select a.login_id;
TempData["Name"] = Result.Single();
return RedirectToAction("Index", "EmployeeHome");
我了解 TempData["Name"] 没有值。我该如何解决这个问题。
最佳答案
TempData 用于将数据从当前请求传递到后续请求(意味着从一个页面重定向到另一个页面)。
因此,在您的情况下,Tempdata[“Name”] 仅在 EmployeeHome Controller 的当前操作和索引操作中可用。
因此您需要 Session[“Name”] 代替 Tempdata[“Name”]
喜欢:
var Result = from a in db.login where a.userName == login.userName &&
a.Password == login.Password select a.login_id;
Session["Name"] = Result.Single();
return RedirectToAction("Index", "EmployeeHome");
还有另一个操作
[HttpPost]
public ActionResult Create(EmployeeEntry employeeentry)
{
…
employeesales.employeeid = (int)Session["Name"];
关于asp.net-mvc - 未将对象引用设置为 TempData 中对象的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27383373/