2011-02-14 171 views
2

使用MVC 3 RTM和MvcContrib/FluentHtml版本3.0.51.0,我無法讓jQuery客戶端驗證工作。服務器端驗證工作正常,並返回顯示正確的驗證摘要等。但是,表單帖子總是嘗試點擊服務器端的帖子控制器操作,當它應該停止在客戶端顯示驗證錯誤。jQuery客戶端驗證不與MVC 3一起使用MvcContrib.FluentHtml

我失蹤的任何想法?最新版本的MvcContrib/FluentHtml能否與jQuery客戶端驗證不兼容?

這裏是我的代碼:

Web.config文件有:

<add key="ClientValidationEnabled" value="true"/> 
<add key="UnobtrusiveJavaScriptEnabled" value="true"/> 

的Site.Master頁面有:

<script src="<%= Url.Content("~/Assets/JavaScript/jquery-1.4.4.min.js")%>" type="text/javascript"></script> 
    <script src="<%= Url.Content("~/Assets/JavaScript/jquery-ui-1.8.7.min.js")%>" type="text/javascript"></script> 
    <script src="<%= Url.Content("~/Assets/JavaScript/jquery.validate.min.js")%>" type="text/javascript"></script> 
    <script src="<%= Url.Content("~/Assets/JavaScript/jquery.validate.unobtrusive.min.js")%>" type="text/javascript"></script> 

查看頁面從MvcContrib的ModelViewPage繼承: 這裏的視圖頁面:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="ModelViewPage<MaintainReferralAgencyDetailVM>" %> 
<%@ Import Namespace="OASIS3G.Controllers" %> 
<%@ Import Namespace="OASIS3G.ViewModels" %> 
<%@ Import Namespace="JCDCHelper.Extension"%> 
<%@ Import Namespace="MvcContrib.FluentHtml" %> 
<%@ Import Namespace="System.Web.Mvc" %> 
<%@ Import Namespace="System.Web.Mvc.Html" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"> 
    <% using (Html.BeginForm()) %> 
    <% { %> 
     <h1>Referral Agency Detail</h1> 
     <%= Html.ValidationSummary(false) %> 
     <table class="NoBorder"> 
      <tr> 
       <td class="NoBorder SubmitFormLeftColumn"><span class="required">* </span>Zip Code:</td> 
       <td class="NoBorder"><%= this.TextBox(x => x.ReferralAgency.ContactInformation.Zipcode) %> Example: 99999 or 999990000 
        <%= Html.ValidationMessageFor(x=>x.ReferralAgency.ContactInformation.Zipcode) %> 
       </td> 
      </tr> 
     </table> 
     <input type="submit" value="Submit" /> 
    <% } %> 
</asp:Content> 

這裏的控制器後動作:

[HttpPost] 
     public ActionResult MaintainReferralAgencyDetail(MaintainReferralAgencyDetailVM userInputs) 
     { 
// I shouldn't reach this when I submit the form with a blank Zipcode, but I do: 
      if (ModelState.IsValid) 
      { 

這裏所要求的領域實體對象:

[Serializable] 
public class ContactInformationEO 
{ 
    public virtual Int64 AddressId { get; set; } 

    [Required] 
    public virtual string Zipcode { get; set; } 

回答

1

剛跑扎進這個問題,貌似Scott Kirkland has a solution

+1

很久以前,我在我的解決方案中翻錄了MvcContrib,但是感謝您發表了這篇文章。我會將其標記爲已解決。如果您想在不依賴MvcContrib的情況下查看包含我的方法的完整解決方案的文章,可以在此處查看:http://robertcorvus.com/automatically-validate-and-format-data-with-asp-net-mvc -and-的jQuery – 2011-05-10 15:08:43

2

我想通了,這絕對是MvcContrib搞亂它。我原本以爲MvcContrib不是一個因素,因爲當我從MvcContrib的ModelViewPage更改爲Mvc.ViewPage時,我將「this.TextBox」切換到「Html.TextBox」,它仍然不起作用。但是,我發現我需要將控件切換爲「Html.EditorFor」而不是「Html.TextBox」。

我已經google了幾分鐘,它看起來不像MvcContrib版本3.0.51.0支持jQuery的客戶端驗證與任何等效的EditorFor,所以我不得不撕裂它,並使用本機MVC控件。

你可以看到一篇文章在這裏我的方法的一個完整的解決方案:automatically-validate-and-format-data-with-asp-net-mvc-and-jquery