나는이 MVC3에서 사용이 클래스 ...IValidatableObject 검증 방법 작동하지
유효성 검사는 유효성 검사 메서드를 호출하고 결과를 반환 할 때 다시 게시되지 예상대로하지만 작업하는 각 속성의 속성 양식에. 아무도 도와 줄 수 있습니까 ??
public class Register : IValidatableObject
{
[DataType(DataType.Text)]
[Display(Name = "District Name")]
[Required(AllowEmptyStrings = false, ErrorMessage = "You must enter a District Name")]
[StringLength(40, MinimumLength = 1, ErrorMessage = "District Name must be between 1 and 40 characters")]
public string DistrictName { get; set; }
[DataType(DataType.Text)]
[Display(Name = "Url Quick Find")]
[Required(ErrorMessage = "You must provide a Quick Find Name")]
[StringLength(15, MinimumLength = 3, ErrorMessage = "Url Quick Find must be between 3 and 15 characters")]
public string QuickFind { get; set; }
[DataType(DataType.Text)]
[Display(Name = "User name")]
[Required(AllowEmptyStrings = false, ErrorMessage = "User name is requred")]
[StringLength(10, MinimumLength = 1, ErrorMessage = "Url Quick Find must be between 1 and 10 characters")]
public string Username { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Password")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Password is requred")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Re-Enter Password")]
[Required(AllowEmptyStrings = false, ErrorMessage = "You must re-enter your password to confirm you have entered it correctly")]
public string PasswordConfirm { get; set; }
[DataType(DataType.Text)]
[Display(Name = "Display Name")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Display Name is requred")]
[StringLength(20, MinimumLength = 1, ErrorMessage = "Display Name must be between 1 and 20 characters")]
public string DisplayName { get; set; }
[DataType(DataType.EmailAddress)]
[Display(Name = "Email Address")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Email Address is requred")]
[StringLength(50, MinimumLength = 4, ErrorMessage = "Email Address must be between 4 and 50 characters")]
public string EmailAddress { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
Regex lettersOnly = new Regex("^[a-zA-Z]*$");
if (!lettersOnly.IsMatch(QuickFind))
yield return new ValidationResult("Only letters A - Z are allowed in the Quick Find", new string[] { "QuickFind" });
if (!lettersOnly.IsMatch(QuickFind))
yield return new ValidationResult("Only letters A - Z are allowed for your User name", new string[] { "Username" });
if (Password != PasswordConfirm)
yield return new ValidationResult("Passwords do not match", new string[] { "Password", "PasswordConfirm" });
}
}
컨트롤러 코드 :
[HttpPost]
public ActionResult Index(Register registration)
{
try
{
User newUser = RegistrationManager.Register(registration);
RedirectToAction("Index", "District", newUser.ID);
}
catch (Exception ex)
{
ModelState.AddModelError("There has been an Error during Registration", ex.Message);
RedirectToAction("Details", "Error", ex);
}
return View();
}
보기 코드 :
@model PubGames.Data.Register
@{
ViewBag.Title = "Register";
}
<h2>
Register</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.DistrictName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DistrictName)
@Html.ValidationMessageFor(model => model.DistrictName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.QuickFind)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.QuickFind)
@Html.ValidationMessageFor(model => model.QuickFind)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Username)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Username)
@Html.ValidationMessageFor(model => model.Username)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PasswordConfirm)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PasswordConfirm)
@Html.ValidationMessageFor(model => model.PasswordConfirm)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DisplayName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DisplayName)
@Html.ValidationMessageFor(model => model.DisplayName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EmailAddress)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EmailAddress)
@Html.ValidationMessageFor(model => model.EmailAddress)
</div>
<p>
<input type="submit" value="Register" />
</p>
}
컨트롤러에 관련 코드를 표시하고보기 만하면됩니다. –
추가 된 컨트롤러와 뷰 코드 –
참고로 코드 샘플에 표시된 유효성 검사에 'IValidatableObject'가 실제로 필요하지 않습니다. 당신은 데이터 주석만으로도 같은 결과를 얻을 수 있습니다 :'[RegularExpression (@ "^ [a-zA-Z] * $", ErrorMessage = "{0}에 문자 A - Z 만 허용됩니다.")'' [Compare ("Password", ErrorMessage = "암호가 일치하지 않습니다.")]'. 데이터 주석은 또한 클라이언트 측 유효성 검사를 제공합니다. –