2012-01-07 78 views
0

我正在使用MVC3構建嚮導步驟演示應用程序,並使用剃鬚刀視圖引擎作爲初始級別。在MVC中使用Razor視圖進行驗證的問題

我遇到了驗證問題時隱藏&通過javascript顯示控件。

請看看我的代碼段按以下

<div class="editor-field"> 
       @Html.EditorFor(model => model.CheckName2) 
       @Html.ValidationMessageFor(model => model.CheckName2) 
      </div> 

我的JavaScript函數按下面,隱藏&表演上的一些條件

// attach nextStep button handler  
     $("#next-step").click(function() { 

      var $step = $(".wizard-step:visible"); // get current step 

      //check if URL2 is having any content   
      var val = $("#URL2").val(); 
      if (val == "") { 
       $("#CheckName2").hide(); 
       //want to remove validation here 

      } 
      else { 

       $("#CheckName2").show(); 
       //want to add validation here 

      } 
var validator = $("form").validate(); // obtain validator 
     var anyError = false; 
     $step.find("input").each(function() { 


      if (!validator.element(this)) { // validate every input element inside this step 

       anyError = true; 
      } 


     }); 

     if (anyError) 
      return false; // exit if any error found 

我怎麼能在這裏辦理驗證?

在此先感謝。

回答

1

使用$(「#IdOfFormYouEdit」)強制驗證您的表單。或$(「#IdOfTextbox」)。validate();僅驗證一個元素。

這是可能的,如果你已經把視圖模型類上的驗證屬性。如果你不這樣做,你可以通過添加data-val屬性來在元素上添加驗證。例如:

<input type="text" data-val="true" data-val-number="The field someField must be a number." data-val-required="SourceId required" /> 
+0

我有問題與form.validation部分更新的JavaScript代碼,請看看,並建議我一些解決方案 – 2012-01-07 13:08:50