public ActionResult Edit()
{
int _userid = 0;
if (Request.Form["UserId"] != null)
{
int.TryParse(Request.Form["UserId"].ToString(), out _userid);
}
SCUser u = _userService.FindById(_userid);
if (u != null)
{
string _userRoleIds = "";
if (Request.Form["UserRoleIds"] != null)
{
_userRoleIds = Request.Form["UserRoleIds"].ToString();
u.UserRoleIds = _userRoleIds;
}
int _userBelongsTo = 0;
if (Request.Form["UserBelongsTo"] != null)
{
int.TryParse(Request.Form["UserBelongsTo"].ToString(), out _userBelongsTo);
u.UserBelongsTo = _userBelongsTo;
}
int _userAdminLevel = 0;
if (Request.Form["UserAdminLevel"] != null)
{
int.TryParse(Request.Form["UserAdminLevel"].ToString(), out _userAdminLevel);
u.UserAdminLevel = _userAdminLevel;
}
int _userStatus = 0;
if (Request.Form["UserStatus"] != null)
{
int.TryParse(Request.Form["UserStatus"].ToString(), out _userStatus);
u.UserStatus = _userStatus;
}
string _useremail = "";
if (Request.Form["UserEmail"] != null)
{
_useremail = Request.Form["UserEmail"].ToString();
u.UserEmail = _useremail;
}
_userService.Edit(u);
return RedirectToAction("Index");
}
return View();
}
CONTROLS代码 public ActionResult Edit() { int _userid = 0; if (Request.Form["UserId"] != null) { int.TryParse(Request.Form["UserId"].ToString(), out _userid); } SCUser u = _userService.FindById(_userid); if (u != null) { string _userRoleIds = ""; if (Request.Form["UserRoleIds"] != null) { _userRoleIds = Request.Form["UserRoleIds"].ToString(); u.UserRoleIds = _userRoleIds; } int _userBelongsTo = 0; if (Request.Form["UserBelongsTo"] != null) { int.TryParse(Request.Form["UserBelongsTo"].ToString(), out _userBelongsTo); u.UserBelongsTo = _userBelongsTo; } int _userAdminLevel = 0; if (Request.Form["UserAdminLevel"] != null) { int.TryParse(Request.Form["UserAdminLevel"].ToString(), out _userAdminLevel); u.UserAdminLevel = _userAdminLevel; } int _userStatus = 0; if (Request.Form["UserStatus"] != null) { int.TryParse(Request.Form["UserStatus"].ToString(), out _userStatus); u.UserStatus = _userStatus; } string _useremail = ""; if (Request.Form["UserEmail"] != null) { _useremail = Request.Form["UserEmail"].ToString(); u.UserEmail = _useremail; } _userService.Edit(u); return RedirectToAction("Index"); } return View(); } 更新 public int Edit(SCUser scuser) { using (_db = new SCenterContext()) { _db.Entry(scuser).State = EntityState.Modified; return _db.SaveChanges(); } }
JS中获取的值错误,所以是undefined。
还有,你这样的写法,完全就没用上mvc的特性。直接将这些做为参数,mvc框架会自动装配的。