【发布时间】:2012-03-14 16:34:50
【问题描述】:
我有一个应用程序,其中有一个用户和零售商帐户。我想实施零售商注册表单的验证。我为零售商制作了一个具有必需内涵的模型,但页面仍然没有显示任何错误输入的消息。任何建议我如何实现我的目标。我正在使用 Asp.net MVC
[PropertiesMustMatch("Password", "ConfirmPassword", ErrorMessage = "The password and confirmation password do not match.")]
public class RegisterStore
{
[Required(AllowEmptyStrings = false, ErrorMessage = "Store Name is Required")]
[DataType(DataType.Text)]
[Display(Name = "Store Name")]
public string Store_Name { get; set; }
.
.
Similarly other properties ...
.
.
}
View 的代码是这样的
<h2>Create a Store Account</h2>
<p>
Use the form below to create a new account.
</p>
<% using (Html.BeginForm())
{%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Retailer Information</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Store_Name) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Store_Name) %>
<%: Html.ValidationMessageFor(model => model.Store_Name) %>
</div>
.
... Similar DIVs for other properties ....
.
.
.
.
<p>
<input type="submit" value="Next" />
</p>
</fieldset>
<% } %>
控制器的代码是这样的
[HttpPost()]
public ActionResult RetailerRegisteration(RegisterStore storeModel)
{
//ViewData["genders"] = Genders;
Debug.WriteLine("Started RetailerRegisteration");
if (string.IsNullOrEmpty(storeModel.UserName))
ModelState.AddModelError(string.Empty, "Please enter Username");
if (string.IsNullOrEmpty(storeModel.Store_Name))
ModelState.AddModelError(string.Empty, "Please enter a store name");
if (!string.IsNullOrEmpty(storeModel.Email) || !storeModel.Email.Contains("@"))
ModelState.AddModelError(string.Empty, "Please enter a valid e-mail address!");
if (string.IsNullOrEmpty(storeModel.Password))
ModelState.AddModelError(string.Empty, "Please enter a Password");
if(! storeModel.Password.Equals(storeModel.ConfirmPassword))
ModelState.AddModelError(string.Empty, "The Passwords must match");
if (ModelState.IsValid)
{
... Create Store Account ....
}
【问题讨论】:
-
当您说“输入错误”时,您的意思是用户将字段留空吗?
-
是的@Harvey ..field 留空
标签: asp.net asp.net-mvc asp.net-mvc-2 validation