[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转换为integer时遇到错误.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 ["名称"]没有价值。我如何解决这个问题。
答案 0 :(得分:3)
TempData用于将数据从当前请求传递到后续请求(意味着从一个页面重定向到另一个页面)。
因此,在您的情况下,Tempdata [“Name”]仅在EmployeeHome Controller的当前操作和Index操作中可用。
所以你需要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"];