2017-05-14 80 views
0

如何檢查validationMessageFor是否爲空?如何檢查是否有任何html.ValidationMessageFor錯誤或不在asp mvc中?

@using (Html.BeginForm("ProfileSetting", "Home", FormMethod.Post, new { @id = "ProfileSetting" })) 
    { 
     <div class="container"> 

      <button type="button" id="btn1" class="btn btn-primary btn-lg">Edit</button><br /> 
      @Html.Label("Username : ") 
      @Html.EditorFor(model => model.Username, new { htmlAttributes = new { @class = "details1 form-control", @placeholder = "Example: Bond007", Type = "text", required = "true", disabled = "true" } }) 
      @Html.ValidationMessageFor(model => model.Username, "", new { @class = "text-danger", @id = "username" }) 
      <br /> 
      @Html.Label("Email : ") 
      @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "details2 form-control", Type = "email", required = "true", disabled = "true" } }) 
      @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" ,id = "email" }) 
      <br /> 
      @Html.Label("Password : ") 
      @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "details3 form-control", @placeholder = "Example: Bond23!", Type = "password", required = "true", disabled = "true" } }) 
      @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" ,id="password" }) 
      <br /> 
      @Html.Label("Date of Birth : ") 
      @Html.EditorFor(model => model.DateOfBirth, "{0:dd-MM-yyyy}", new { htmlAttributes = new { @class = "details4 form-control" ,@placeholder="DD-MM-YYYY",Type = "text", required = "true", disabled = "true" } }) 
      @Html.ValidationMessageFor(model => model.DateOfBirth, "", new { @class = "text-danger", id = "dob" }) 
      <br /> 
      @Html.Label("Phone No : ") 
      @Html.EditorFor(model => model.PhoneNo, new { htmlAttributes = new { @class = "details5 form-control", Type = "number", @placeholder = "Phone No", required = "true", disabled = "true" } }) 
      @Html.ValidationMessageFor(model => model.PhoneNo, "", new { @class = "text-danger", id = "phoneno" }) 
      <br /> 
      <label id="Successfully_Changed"></label> 
      <button type="button" id="btnSave" class="btn btn-primary btn-lg">Save</button> 
      <br /> 
     </div> 
    } 

腳本:

$('#btnSave').click(function() { 

       @{ 
      if (!String.IsNullOrEmpty(@Html.ValidationMessageFor(u=>u.Username).ToString())) 
        { 
         <text> 
         alert($('#ProfileSetting').validate().valid()); 
        $.post(
         "/Home/EditProfile", 
           { 
          Username: $('.details1').val(), 
          Password: $('.details3').val(), 
          DateOfBirth: $('.details4').val(), 
          PhoneNo: $('.details5').val(), 
         } 
        ); 
        $('#Successfully_Changed').html("Successfully Changed."); 
        $('#Successfully_Changed').show(); 
       </text> 
         } } 
      }); 
+1

爲什麼你想這樣做? – CodingYoshi

+0

如果沒有任何錯誤,要對控制器進行ajax調用,而不是隱藏保存按鈕並將數據保存在數據庫的h字段中並禁用該字段。 –

回答

0

在一個評論,你的問題,你說:

爲了使Ajax調用控制器,如果它沒有任何錯誤比隱藏保存按鈕並將數據保存在數據庫的h字段中並禁用該字段。

如果這是你的目標,以檢查的形式是有效的,你做一個AJAX調用之前,那麼這是做它的方式:

var form = $("#formId"); 
form.validate(); 
if (form.valid()){ 
    // All is good so do your ajax operation. 
} 

valid()方法:

檢查所選表單是否有效或所有選定元素是否有效。

相關問題