2016-10-27 73 views
1

我需要幫助。 我有一個用戶註冊表單,我必須將「客戶」與用戶映射。asp.net MVC與jQuery的遠程驗證

現在我想驗證來自另一個來源的用戶「客戶」,並且我將「客戶」放入選擇列表「客戶」超過2000年,這就是爲什麼我使用JQuery選擇插件在選擇列表中搜索 但「客戶」字段取決於「角色」,這就是爲什麼在頁面加載時,當我更改角色「客戶」字段(選擇的選擇列表)顯示以及當我選擇客戶時它沒有觸發遠程驗證時,默認隱藏「客戶」字段。 我試圖讓它在「檢查元素」上可見,我更改顯示:無顯示:bock,並嘗試更改從選擇它不工作的值,當我更改選擇列表中的orignal選擇列表值,然後其工作正常我的意思是它發射我的遠程驗證器方法在這裏是完整的代碼示例我在做什麼。 請幫助我想驗證當選擇選擇列表值更改。

這是RegisterViewModel

public class RegisterViewModel 
    { 
     [Required] 
     [Display(Name = "Role")] 
     public string Role { get; set; } 

     //for edit view model additionalFields which will only require for edit mode 
     //[System.Web.Mvc.Remote("DoesCustomerCodeExist", "Account", AdditionalFields = "OldCustomerCode")] 
     [Required(AllowEmptyStrings = false, ErrorMessage = "Customer Code is required.")] 
     [Display(Name = "Customer Code", Description = "A customer code come from our oracle system.")] 
     [System.Web.Mvc.Remote("DoesCustomerCodeExist", "Account")] 
     [Range(0, int.MaxValue, ErrorMessage = "Please enter valid Customer Code in number only.")] 
     public string CustomerCode { get; set; } 

    } 

下面是在這個文件我看來CSHTML也有js代碼來顯示用戶選擇的選擇列表時,角色發生了轉變。

//select Role 
<div class="form-group"> 
    @Html.LabelFor(m => m.Role, new { @class = "col-md-2 control-label" }) 
    <div class="col-md-10"> 
     @Html.DropDownListFor(x => x.Role, ViewBag.Roles as SelectList,"", new { @class = "form-control chosen-select", data_placeholder = "Select a Role" }) 
     @Html.ValidationMessageFor(m => m.Role, "", new { @class = "text-danger" }) 
    </div> 
</div> 

//Customer Code 
<div class="form-group condition-div user hidden "> 
    //this hidden field is only for edit mode 
    //@Html.Hidden("OldCustomerCode", Model.CustomerCode) 
    @Html.LabelFor(m => m.CustomerCode, new { @class = "col-md-2 control-label" }) 
    <div class="col-md-10"> 
     @Html.DropDownListFor(x => x.CustomerCode, (SelectList)ViewBag.Customers, "", new { @class = "form-control chosen-customers", data_placeholder = "Select Customer" }) 
     @Html.ValidationMessageFor(m => m.CustomerCode, "", new { @class = "text-danger" }) 
    </div> 
</div> 


@section Styles{ 
    @Styles.Render("~/Content/chosen") 
} 

@section Scripts { 
@Scripts.Render("~/bundles/jqueryval") 
@Scripts.Render("~/bundles/chosen") 

<script type="text/javascript"> 
     $('input[type=text]').tooltip(
       { 
        placement: "right", 
        trigger: "focus" 
       } 
      ); 

     $(".chosen-select").chosen({ allow_single_deselect: true}); 

     $('#Role').change(function() { 

      if (this.value == "") { 
       $('.condition-div').addClass('hidden'); // hide all the conidional divs 
      } else if (this.value == "NBP User" || this.value == "NBP Head") { 
       $('.condition-div.admin').addClass('hidden'); /// hide admin conditional divs 
       $('.condition-div.user').removeClass('hidden'); // show user role conditioanl div 
       //configure selectlist to Chosen select and if i remove this line and show orignal select list its working fine mean remote validating on change but if i use this is not working on change. 
       $(".chosen-customers").chosen({ allow_single_deselect: true, search_contains: true }); 
       $.validator.setDefaults({ ignore: ":hidden:not(.chosen-customers)" }); 
      } else if (this.value == "ICIL User" || this.value == "ICIL Head" || this.value == "FIO User") { 
       $('.condition-div.user').addClass('hidden'); /// hide user role conditional divs 
       $('.condition-div.admin').removeClass('hidden'); // show admin role conditional divs 
       $(".chosen-branch").chosen({ allow_single_deselect: true }); 
       $.validator.setDefaults(); 
      } 
     }); 
</script> 
} 

控制器動作,以驗證客戶代碼

public ActionResult DoesCustomerCodeExist(string CustomerCode, string OldCustomerCode) 
    { 
     //the oldCustomerCode will come null in this case cause its register view and in edit view OldCustomerCode will be use 
     if (CustomerCode == OldCustomerCode) 
      return Json(true, JsonRequestBehavior.AllowGet); 

     if (DbContext.Users.Any(x => x.CustomerCode == CustomerCode)) 
      return Json("Customer code already exists in application. Please verify user details.", JsonRequestBehavior.AllowGet); 

     if (DbOracle.IsCustomerCodeExist(CustomerCode)) 
      return Json(true, JsonRequestBehavior.AllowGet); 
     else 
      return Json("The customer code does not exist in database.", JsonRequestBehavior.AllowGet); 
    } 

所有的代碼工作正常,如果我不使用jquery選擇插件。 簡而言之,當我使用選擇插件的選擇列表遠程驗證停止驗證值。 我可以分享圖片,如果你們現在需要我有一個有限的帳戶,所以我不能上傳快照截圖.... 請幫助我。

+0

我用OldCustomerCode在我的編輯視圖和工作正常沒有選擇當我使用選擇它會停止驗證 –

+0

@StephenMuecke爲什麼你標記爲重複? –

+0

是的,我閱讀所有這些答案,我在其他應用程序中實現,那些工作正常,但那些驗證是需要或匹配類型沒有遠程驗證,你可以閱讀該行$ .validator.setDefaults({ignore:「:hidden: not(.chosen-customers)「});它的意思是忽略除了「.chosen-customers」這個類別之外的所有內容。我在角色轉換時使用它。 –

回答

1

你應該把在客戶端的一些JQuery的跟蹤「CustomerCode」字段時,客戶領域的變化僅僅指剛稱之爲「事件的內容()」的「CustomerCode」,例如事件:

$('#CustomerCode').change(function() { 
     $(this).focusout(); 
    });