2013-03-30 157 views
2

我在我的應用程序中有一個頁面,我已經使用validate.min.jsvalidate.unobtrusive.min.js來應用MVC客戶端驗證,但它不起作用。驗證在服務器端正常工作,但不在客戶端。我認爲我用於菜單的js可能會造成問題。但我無法理解如何解決它,因爲我使用的菜單需要菜單效果。MVC3客戶端驗證不起作用

任何人如果有任何想法請分享。

代碼視圖頁:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ApricaCRMEvent.Models.User_Detail>" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
    RegisterUser1 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

<h2>RegisterUser1</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) %> 
    <fieldset> 
     <legend>User_Detail</legend> 

     <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.Email_Add) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.EditorFor(model => model.Email_Add) %> 
      <%: Html.ValidationMessageFor(model => model.Email_Add) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.Alt_Email_Add) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.EditorFor(model => model.Alt_Email_Add) %> 
      <%: Html.ValidationMessageFor(model => model.Alt_Email_Add) %> 
     </div> 

     <div class="editor-label"> 
      <%: Html.LabelFor(model => model.ConfirmPassword) %> 
     </div> 
     <div class="editor-field"> 
      <%: Html.EditorFor(model => model.ConfirmPassword) %> 
      <%: Html.ValidationMessageFor(model => model.ConfirmPassword) %> 
     </div> 

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

<div> 
    <%: Html.ActionLink("Back to List", "Index") %> 
</div> 

</asp:Content> 

<asp:Content ID="Content3" ContentPlaceHolderID="HeadContent" runat="server"> 
</asp:Content> 

它生成的html:

<!DOCTYPE html> 

<html> 
<head id="Head1"><title> 

    RegisterUser1 

</title><link href="../Content/Site.css" rel="stylesheet" type="text/css" /> 

    <script src="/Scripts/jquery-1.9.1.min.js" type="text/javascript"></script> 
    <script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script> 

    <!-- for Menu (Imp: jquery.js and menu.js must be included at last in links for menu effects to work.)--> 
    <link href="../Content/menu.css" rel="stylesheet" type="text/css" /> 
    <script src="../../Scripts/jquery.js" type="text/javascript"></script> 
    <script src="../../Scripts/menu.js" type="text/javascript"></script> 
    <!--- for menu end --> 

</head> 
<body> 
    <div class="page"> 
     <header> 

      <table id="header"> 

       <tr> 
        <td> 
         <div id="aprica_logo"> 

          <img src="../../Content/images/Apricalogo.png" alt="Aprica" style="width: 134px; 
           height: 100px;" /> 
         </div> 
        </td> 
        <td> 
         <div align="center" id="title"> 
          <h1> 
           Aprica</h1> 
         </div> 
        </td> 
        <td> 
         <div id="logindisplay"> 

     Welcome <strong>admin</strong> 
     [ <a href="/CRMLogin/LogOff">Log Out</a> ] 

         </div> 
        </td> 
       </tr> 
      </table> 
      <div id="menu"> 
       <ul class="menu"> 
        <li><a href="#" class="parent"><span>My Account</span></a> 
         <div> 
          <ul> 
           <li><a href="#"><span>Change Password</span></a></li> 
           <li><a href="#"><span>Message Passing</span></a></li> 
           <li><a href="#"><span>Edit Profile</span></a></li> 
          </ul> 
         </div> 
        </li> 
        <li> 
         <a href="/CRMDashboard/Dashboard"> 
          <span>Dashboard</span></a></li> 
        <li><a href="/CRM/Index" class="parent"><span>CRM Request</span></a> 
         <div> 
          <ul> 
           <li><a href="/CRM/AddNewCRM"><span>Add New CRM Request</span></a></li> 
           <li><a href="/CRM/ViewNewlyArrivedCRMRequest"><span>CRM Request List</span></a></li> 
           <li><a href="/CRM/NewOldDoctor"><span>CRM Doctors List</span></a></li> 
           <li><a href="/CRM/AddCRMDoctorBudget"><span>Add New CRM Doctor</span></a></li> 
           <li><a href="/CRM/UpdateCRMDoctorBudgetMain"><span>Edit CRM Doctor</span></a></li> 
          </ul> 
         </div> 
        </li> 
        <li><a href="#" class="parent"><span>Reports</span></a> 
         <div> 
          <ul> 
           <li><a href="#" class="parent"><span>Chart Reports</span></a> 
            <div> 
             <ul> 
              <li><a href="/ChartReport/Index"><span>CRM Request Reports</span></a> 
              </li> 
             </ul> 
            </div> 
           </li> 
           <li><a href="/Report/CRMStatus"><span>CRM Request Status</span></a></li> 
           <li><a href="/Report/DoctorReport"><span>Doctor wise</span></a></li> 
          </ul> 
         </div> 
        </li> 
        <li><a href="#" class="parent"><span>Search</span></a> 
         <div> 
          <ul> 
           <li><a href="/Search/CRMIdIndex"><span>CRM Request Id Wise</span></a></li> 
           <li><a href="/Search/MDLNoIndex"><span>MDLNo Wise</span></a></li> 
           <li><a href="/Search/TerritoryIndex"><span>Territory Wise</span></a></li> 
           <li><a href="/Search/RequesterIndex"><span>TBM Wise</span></a></li> 
           <li><a href="/Search/PriorityIndex"><span>Priority Wise</span></a></li> 
           <li><a href="/Search/CRMStateIndex"><span>CRM State Wise</span></a></li> 
           <li><a href="/Search/ServiceStateIndex"><span>Service State Wise</span></a></li> 
          </ul> 
         </div> 
        </li> 
        <li><a href="/FilterCRMRequest/Index"><span>Filter CRM Requests</span></a></li> 
        <li><a href="#"><span>Settings</span></a></li> 
       </ul> 
      </div> 
     </header> 
     <section id="main"> 


<h2>RegisterUser1</h2> 

<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script> 
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script> 

<form action="/CRMLogin/RegisterUser1" method="post"> 
    <fieldset> 
     <legend>User_Detail</legend> 

     <div class="editor-label"> 
      <label for="Username">Username</label> 
     </div> 
     <div class="editor-field"> 
      <input class="text-box single-line" data-val="true" data-val-required="The Username field is required." id="Username" name="Username" type="text" value="" /> 
      <span class="field-validation-valid" data-valmsg-for="Username" data-valmsg-replace="true"></span> 
     </div> 

     <div class="editor-label"> 
      <label for="Password">Password</label> 
     </div> 
     <div class="editor-field"> 
      <input class="text-box single-line password" data-val="true" data-val-required="The Password field is required." id="Password" name="Password" type="password" value="" /> 
      <span class="field-validation-valid" data-valmsg-for="Password" data-valmsg-replace="true"></span> 
     </div> 

     <div class="editor-label"> 
      <label for="Email_Add">Email address</label> 
     </div> 
     <div class="editor-field"> 
      <input class="text-box single-line" data-val="true" data-val-required="The Email address field is required." id="Email_Add" name="Email_Add" type="text" value="" /> 
      <span class="field-validation-valid" data-valmsg-for="Email_Add" data-valmsg-replace="true"></span> 
     </div> 

     <div class="editor-label"> 
      <label for="Alt_Email_Add">Alternate Email address</label> 
     </div> 
     <div class="editor-field"> 
      <input class="text-box single-line" data-val="true" data-val-required="The Alternate Email address field is required." id="Alt_Email_Add" name="Alt_Email_Add" type="text" value="" /> 
      <span class="field-validation-valid" data-valmsg-for="Alt_Email_Add" data-valmsg-replace="true"></span> 
     </div> 

     <div class="editor-label"> 
      <label for="ConfirmPassword">Confirm password</label> 
     </div> 
     <div class="editor-field"> 
      <input class="text-box single-line password" data-val="true" data-val-equalto="The password and confirmation password do not match." data-val-equalto-other="*.Password" id="ConfirmPassword" name="ConfirmPassword" type="password" value="" /> 
      <span class="field-validation-valid" data-valmsg-for="ConfirmPassword" data-valmsg-replace="true"></span> 
     </div> 

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

      <footer> 
       <hr /> 
       <p> 
        Copyright (c) 2012</p> 
       <hr /> 

      </footer> 
     </section> 
    </div> 
</body> 
</html> 
+1

check in web.config ClientValidationEnabled設置爲true。 – ssilas777

+0

它被設置爲真...我檢查了它 – blue

+0

打開瀏覽器上的控制檯,你使用(打F12)看看是否有任何客戶端異常,因爲我看到你第一次引用jQuery兩次 –

回答

0

同樣的事情也自然發生在我身上,我能夠重新排序的腳本來解決它。如果所有其他選項用完,您可以嘗試此操作。

的順序是

Jquery-yourversion.js 
MicrosoftAjax.js 
MicrosoftMvcAjax.js 
MicrosoftMvcValidation.js 
YourOtherScripts.js 

我從另一個網頁後此提示,但是現在無法找到它,包括對它的引用。如果這能起作用,那麼信貸就會發給那個職位的作者。

希望這可以幫到你

+0

對不起......它沒有幫助。在這裏,我用於菜單的jquey文件正在爲我創建問題。我不能改變菜單,因爲我的senoir很喜歡它... – blue

0

我有同樣的客戶端驗證不起作用的問題。我在Chrome中調用JavaScript控制檯,看到我收到一個錯誤,指出「JQuery沒有定義。」。

原來我的View中有一些代碼導致jQuery沒有加載時出現問題。

建議其他人遇到這種情況,請檢查您的瀏覽器中的JS控制檯,以確保您沒有得到一個JQuery錯誤。