2011-12-29 100 views
1

我有這個類中MVC3使用...IValidatableObject validate方法不工作

的屬性驗證,每個屬性都工作良好,但是當驗證方法被調用任何結果返回它不回發到表格。 任何人都可以幫忙嗎?

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> 
} 
+1

你還需要在控制器中顯示相關的代碼,並查看。 – 2011-12-29 02:24:24

+0

添加控制器和視圖代碼 – 2011-12-29 09:10:13

+0

僅供參考,您並不需要'IValidatableObject'來驗證代碼示例中顯示的驗證;您可以單獨使用數據註釋獲得相同的結果:'[RegularExpression(@「^ [a-zA-Z] * $」,ErrorMessage =「只允許{0}中的字母A-Z。」)]'和' [比較(「密碼」,ErrorMessage =「密碼不匹配。」)]'。數據註釋還爲您提供客戶端驗證。 – 2012-01-14 22:55:15

回答

1

這麼多的問題...

的典型操作方法是這樣的:

public ActionResult DoSomething(MyModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     return RedirectToAction("somethign"); 
    } 
    return View(model); 
} 

這確實幾件事情,但主要是返回到視圖模式狀態下,如果它是無效的。你沒有這樣做,實際上你甚至不會將模型返回到視圖。您不檢查狀態是否有效,因此即使驗證服務器未通過驗證,它也會嘗試調用註冊方法。你也沒有返回你的ActionResults,即RedirectToAction,你只是在調用它..

+0

ModelState.IsValid - 謝謝! – 2011-12-29 10:08:57

0

你的意思是 「不張貼回表」 - 你的意思是在」錯誤t出現? 確保

  1. 你的頁面
  2. 在驗證摘要你創建信息後沒有進行重定向。
  3. 知道這是不是客戶端驗證(只是說,你可能已經知道)
1

你沒有正確重定向。您必須返回 RedirectToAction:

[HttpPost] 
public ActionResult Index(Register registration) 
{ 
    try 
    { 
     User newUser = RegistrationManager.Register(registration); 
     return RedirectToAction("Index", "District", newUser.ID); 
    } 
    catch (Exception ex) 
    { 
     ModelState.AddModelError("There has been an Error during Registration", ex.Message); 
     return RedirectToAction("Details", "Error", ex); 
    } 

    return View(registration); 
} 

此外,當您在例外的情況下重定向你會失去你已經添加到ModelState中的任何錯誤。