2012-07-24 135 views
0

我正在使用ASP.NET MVC3和EF 4.1 我有兩個DropDownList在我的模型中,它是必需的,也沒有重複。 而我想要遠程驗證功能:ValidateDuplicateInsert在用戶提交數據時觸發。但我無法獲得ValidateDuplicateInsert函數觸發。 我錯在哪裏?遠程驗證DropDownList,MVC3,在我的情況下沒有觸發

我的模型

[Key] 
    public int CMAndOrgID { get; set; } 

    [Display(Name = "CM")] 
    [Required(ErrorMessage = "CM is required.")] 
    [Remote("ValidateDuplicateInsert", "CMAndOrg", HttpMethod = "Post", AdditionalFields = "CMID, OrganizationID", ErrorMessage = "CM is assigned to this Organization.")] 
    public int? CMID { get; set; } 

    [Display(Name = "Organization")] 
    [Required(ErrorMessage = "Organization is required.")] 
    public int? OrganizationID { get; set; } 

    public virtual CM CM { get; set; } 
    public virtual Organization Organization { get; set; } 

的ValidateDuplicateInsert功能在我CMAndOrg控制器

[HttpPost] 
    public ActionResult ValidateDuplicateInsert(string cmID, string orgID) 
    { 
     bool flagResult = true; 
     foreach (CMAndOrg item in db.CMAndOrgs) 
     { 
      if (item.CMID.ToString() == cmID && item.OrganizationID.ToString() == orgID) 
      { 
       flagResult = false; 
       break; 
      } 
     } 
     return Json(flagResult); 
    } 

而且我查看

@using (Html.BeginForm()) { 
@Html.ValidationSummary(true) 
<fieldset> 
    <legend>CMAndOrg</legend> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.CMID, "CM") 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownList("CMID", String.Empty) 
     @Html.ValidationMessageFor(model => model.CMID) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.OrganizationID, "Organization") 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownList("OrganizationID", String.Empty) 
     @Html.ValidationMessageFor(model => model.OrganizationID) 
    </div> 

    <p> 
     <input type="submit" value="Create" /> 
    </p> 
</fieldset> 
} 

回答

0

有相關的上下拉列表不引人注目的驗證在MVC3的錯誤。請參考此http://aspnet.codeplex.com/workitem/7629[^]鏈接瞭解更多詳細說明。

簡單地說,您不能使用類的收集和分類字段的名稱相同,所以只是改變按照你的觀點一致的集合名稱和更新

@Html.DropDownList("CategoryID", String.Empty) 

與此

@Html.DropDownListFor(model => model.CategoryID, new SelectList((System.Collections.IEnumerable)ViewData["Categories"], "Value", "Text")) 

再次感謝Henry He

原創鏈接 http://www.codeproject.com/Articles/249452/ASP-NET-MVC3-Validation-Basic?msg=4330725#xx4330725xx

相關問題