首页 新闻 会员 周边

.net mvc 数据更新问题 困扰我半个月了

0
悬赏园豆:100 [已解决问题] 解决于 2009-02-21 09:21

我的view:

                <%using (Html.BeginForm())
{
%>
<p>
<label for="username">
用户名称:
</label>
<%=p.UserName%>
<%= Html.Hidden("UserName")%>
<%= Html.ValidationMessage("UserName")%>
</p>
<p>
<label for="userpassword">
用户密码:
</label>
<%= Html.Password("userpassword")%>
<%= Html.ValidationMessage("userpassword")%>
</p>
<p>
<label for="Email">
电子邮件:
</label>
<%= Html.TextBox("Email")%>
<%= Html.ValidationMessage("Email")%>
</p>
<p>
<label for="Comment">
特定信息:
</label>
<%= Html.TextBox("Comment")%>
<%= Html.ValidationMessage("Comment")%>
</p>
<p>
<label>
注册日期:
</label>
<%= p.CreationDate%>
</p>
<p>
<input type="submit" value="修改" />
</p>
<%}%>

 

controller:

        public ActionResult UserInfo(string id)
{
ViewData[
"AllRoles"] = Roles.GetAllRoles();
MembershipUser a
= Membership.GetUser(id);
if (a == null)
{
return RedirectToAction("Err/用户名不存在");
}
var arr
= new ArrayList();
arr.AddRange(Roles.GetRolesForUser(id));
ViewData[
"UserRoles"] = arr;
return View(a);
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UserInfo(string id, FormCollection Form)
{
ViewData[
"AllRoles"] = Roles.GetAllRoles();
MembershipUser a
= Membership.GetUser(id);
if (a == null)
{
return RedirectToAction("Err/用户名不存在");
}
var arr
= new ArrayList();
arr.AddRange(Roles.GetRolesForUser(id));
ViewData[
"UserRoles"] = arr;
if (string.IsNullOrEmpty(Form["Email"]))
{
ViewData.ModelState.AddModelError(
"Email", "请填写电子邮件");
}
else
{
/*if (Membership.GetUserNameByEmail(Form["Email"]) != null)
{
if (!(Membership.GetUserNameByEmail(Form["Email"]) == a.UserName))
{
ViewData.ModelState.AddModelError("Email", "电子邮件已经被占用");
}
}
*/
}

if (!ViewData.ModelState.IsValid)
{
ViewData[
"Email"] = "@";
return View(a);
}
a.Email
= Form["Email"];
Membership.UpdateUser(a);
return RedirectToAction("UserInfo/" + id);
}


当电子邮件值为空提交的时候出现一下内容,请问高手如何解决
 

“/”应用程序中的服务器错误。

未将对象引用设置到对象的实例。

说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:

行 35:                     <label for="Email">
行 36:                         电子邮件:</label>
行 37:                     <%= Html.TextBox("Email")%>
行 38:                     <%= Html.ValidationMessage("Email")%>
行 39:                 </p>

源文件: g:\200901mvc\mvc\Views\Admin\UserInfo.aspx    行: 37
刘晓君的主页 刘晓君 | 初学一级 | 园豆:57
提问于:2009-02-20 16:56
< >
分享
最佳答案
0

 if (!ViewData.ModelState.IsValid)            {               
//ViewData["Email"] = "@";   
a.Email="@";           
return View(a);           
}

试试看,好像是先从ViewData.Model中取值来绑定的

Q.Lee.lulu | 小虾三级 |园豆:565 | 2009-02-20 23:55
其他回答(1)
0

up,不过真的没见过这种和java一样象的开发模式,关注中

号手 | 园豆:220 (菜鸟二级) | 2009-02-20 20:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册