首页 新闻 会员 周边 捐助

MVC3 数据更新完成后 增加条 UNFINDE

0
悬赏园豆:10 [已解决问题] 解决于 2013-06-20 15:17

  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();
            }
        }
问题补充:

修改完成后,会在数据库里面 增加一条数据 用户名 undefined 的数据

野人_的主页 野人_ | 初学一级 | 园豆:197
提问于:2013-06-20 09:48
< >
分享
最佳答案
0

JS中获取的值错误,所以是undefined。

还有,你这样的写法,完全就没用上mvc的特性。直接将这些做为参数,mvc框架会自动装配的。

收获园豆:10
幻天芒 | 高人七级 |园豆:37207 | 2013-06-20 13:02
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册