ASP.NET MVC Html.TextBox 报 Object reference not set to an instance of an object 的解决方案
2009-07-14 16:05
736 查看
Control中代码如下:
2 [AcceptVerbs(HttpVerbs.Post)]
3 public ActionResult Register(FormCollection collection)
4 {
5 string userName = collection["UserName"];
6 if (string.IsNullOrEmpty(userName.Trim()))
7 {
8 ModelState.AddModelError("UserName", "用户名不能为空!");
9 ModelState.SetModelValue("UserName", collection.ToValueProvider()["UserName"]);
10 }
11 else if (pwd != confirmPassword)
12 {
13 ModelState.AddModelError("ConfirmPassword", "密码不一致!");
14 ModelState.SetModelValue("ConfirmPassword", collection.ToValueProvider()["ConfirmPassword"]);
15 }
16 else
17 {
18 userInfoBusiness.AddUser(userInfo);
19 DoLogin(userName, pwd);
20 return RedirectToAction("Index", "Home");
21 }
22 return View(userInfo);
23 }
关键是要加: ModelState.SetModelValue("UserName", collection.ToValueProvider()["UserName"]);
2 [AcceptVerbs(HttpVerbs.Post)]
3 public ActionResult Register(FormCollection collection)
4 {
5 string userName = collection["UserName"];
6 if (string.IsNullOrEmpty(userName.Trim()))
7 {
8 ModelState.AddModelError("UserName", "用户名不能为空!");
9 ModelState.SetModelValue("UserName", collection.ToValueProvider()["UserName"]);
10 }
11 else if (pwd != confirmPassword)
12 {
13 ModelState.AddModelError("ConfirmPassword", "密码不一致!");
14 ModelState.SetModelValue("ConfirmPassword", collection.ToValueProvider()["ConfirmPassword"]);
15 }
16 else
17 {
18 userInfoBusiness.AddUser(userInfo);
19 DoLogin(userName, pwd);
20 return RedirectToAction("Index", "Home");
21 }
22 return View(userInfo);
23 }
关键是要加: ModelState.SetModelValue("UserName", collection.ToValueProvider()["UserName"]);
相关文章推荐
- Visual Studio 2015打开ASP.NET MVC的View提示"Object reference not set to an instance of an object"错误的解决方案
- VB.NET Error Object reference not set to an instance of an object
- NET:Error Creating Control -"Object Reference Not Set To An Instance Of Object"
- Sharepoint timer job:Object reference not set to an instance of an object
- This qt version uses an unsupported makefile generator object reference not set to an instance of an
- 解决调用context.Session["NAME"]时总出现Object reference not set to an instance of an object.异常的方法
- vs数据库访问出现Object reference not set to an instance of an object. 错误解决办法
- Object reference not set to an instance of an object.
- 20100507 学习记录: Server Error in '/' Application. Object reference not set to an instance of an object
- Build (web): Object reference not set to an instance of an object
- silverlight安装后仍然提示安装以及打开vs的xaml页面提示Object reference not set to an instance of an object 错误
- 我遇到的Object reference not set to an instance of an object.错误的解决办法
- Object reference not set to an instance of an object.
- 添加wcf服务时弹出"an object reference not set to an instance of an object "
- VS2005 solution文件打开时弹出“object reference not set to an instance of an object”错误的解决方法
- Object reference not set to an instance of an object
- Object reference not set to an instance of an object.
- 关于Unity3D的错误:NullReferenceException: Object reference not set to an instance of an object 的解答
- NullReferenceException: Object reference not set to an instance of an object
- [BTS]Object reference not set to an instance of an object