自动填充
public ActionResult Register2(UserInfo userInfo)//自动填充(如果表单中表单元素的name属性的取值与实体类中属性的名字保持一致,会自动填充。)
{
userInfo.RegTime = DateTime.Now;
Test3Entities db = new Test3Entities();
db.UserInfo.Add(userInfo);
db.SaveChanges();
return Content("ok");
}
public ActionResult Register3(string txtName, string txtPwd)//表单中表单元素的name属性取值与当前方法中参数的名称一致,会自动填充。
{
UserInfo userInfo = new UserInfo();
userInfo.UserName = txtName;
userInfo.UserPwd = txtPwd;
userInfo.RegTime = DateTime.Now;
Test3Entities db = new Test3Entities();
db.UserInfo.Add(userInfo);
if (db.SaveChanges() < 0)
{
return Content("添加成功");
}
else
{
//return Content("添加失败!!");
// ViewData["msg"] = "添加失败!!";//控制可以通过ViewData将输入传递到视图.
// ViewData["aa"] = userInfo;
// return Redirect("/Home/Index");//Response.Redirect();
return RedirectToAction("Index", "Home", new { msg = "添加失败!!",txt="aaa"});//跳转到Index方法.
}
}