我的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);
}
|
if (!ViewData.ModelState.IsValid) {
//ViewData["Email"] = "@";
a.Email="@";
return View(a);
}
试试看,好像是先从ViewData.Model中取值来绑定的
up,不过真的没见过这种和java一样象的开发模式,关注中