【发布时间】:2020-11-05 21:55:58
【问题描述】:
我需要知道如何从另一个方法调用/使用字符串。 我想将数据从 User 表传输到 Employee 表。我已经设置了从用户表到员工表的外键 ID。但是登录用户后,他们输入了他们的信息,所以它会自动将Id从User表输入到Employee表。
我的代码如下。
public class EmpInputController : Controller
{
int idd;
[HttpPost]
public ActionResult Login(Models.Member model)
{
using (var context = new GM_demoEntities())
{
bool isValid = context.User.Any(x => x.UserName == model.UserName && x.Password ==
model.Password);
var userr = db.User.Where(x => x.UserName == model.UserName && x.Password ==
model.Password).FirstOrDefault();
if (userr==null)
{
ModelState.AddModelError("", "Invalid username and password");
FormsAuthentication.SetAuthCookie(model.UserName, false);
}
else
{
idd = userr.Id; **//I defined this value for moving Id to another table.**
return RedirectToAction("Empinput", "Empinput");
}
}
return View();
}
public ActionResult Empinput(Employee model)
{
using (var context = new GM_demoEntities())
{
Employee Infoma = new Employee();
Infoma.Id = idd; **//Defined idd to Employee table.**
context.Employee.Add(model);
context.SaveChanges();
}
return RedirectToAction("Login");
}
}
该代码不起作用。 idd总是0,所以我输入Employee后,0只能输入Employee表。
【问题讨论】:
-
标记为 SQL 但您已发布 C#。您要么需要更改标签,要么共享您的 SQL,以便获得所需的帮助。