我正在研究 mvc - 3。我创建了一个寄存器模型和一个寄存器视图。
注册视图:
@using (Html.BeginForm())
{
@Html.ValidationSummary(true, "Correct the errors and try again.")
<div>
<fieldset>
<legend>Account Information</legend>
<div class="editor-label">
@Html.LabelFor(m => m.UserName)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Email)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.Email)
@Html.ValidationMessageFor(m => m.Email)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.ConfirmPassword)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.ConfirmPassword)
@Html.ValidationMessageFor(m => m.ConfirmPassword)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Question)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => m.Question, new SelectList(Model.Questions))
@Html.HiddenFor(m => m.Questions)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Answer)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.Answer)
@Html.ValidationMessageFor(m => m.Answer)
</div>
<p>
<input type="submit" value="Register" />
</p>
</fieldset>
</div>
}
注册模型
public class RegisterModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[RegularExpression(@"^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$", ErrorMessage="Invalid email address")]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
[Display(Name = "Security question")]
public string Question { get; set; }
[Display(Name = "Security question")]
public List<string> Questions { get; set; }
[Display(Name = "Security answer")]
public string Answer { get; set; }
}
我的问题是,当我们将注册表单提交到服务器时,除了问题列表之外,所有数据都会发送到服务器。因此,如果验证失败,那么我得到的异常Model.Questions
是空的。我该如何解决这个问题?