无法在Partial View工作中使用ajax.beginForm进行jquery验证

时间:2010-08-24 16:57:19

标签: jquery asp.net validation asp.net-mvc-2

我正在加载部分视图,这些视图是ajax Forms。 他们看起来像这样

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<WebGRC.Web.Controllers.Models.Administration.UserAdminModel>" %>
<%@ Import Namespace="WebGRC.Web.Controllers.Models.User" %>

<h2><%=Html.Encode(ViewData["HeaderText"])%></h2>

<% Html.EnableClientValidation(); %>

<% using (Ajax.BeginForm(ViewData["PostAction"].ToString(), "Administration", 
       new AjaxOptions() { 
           HttpMethod = "Post",
           UpdateTargetId = "admin-main" 
       }))
   { %>


    <%= Html.ValidationSummary(true, ViewData["ValidationSummaryText"].ToString()) %>

    <div>
        <fieldset>
            <legend>Informations Système</legend>

            <%= Html.EditorFor(model=>model.Username)%>
            <%= Html.EditorFor(model=>model.Email)%>

            <%= Html.Encode(ViewData["PasswordText"])%>
            <br />

            <%= Html.EditorFor(model=>model.Password)%>
            <%= Html.EditorFor(model=>model.ConfirmPassword)%>

        </fieldset>

        <%--<%= Html.EditorFor(model=>model.UserInfo) %>--%>

        <p>
            <input type="submit" value="Register" />
        </p>
    </div>

<% } %>

Index.aspx的主要形式如下所示

<%@ Page Language="C#"  Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="WebGRC.Web.Controllers" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <link href="<%= ResolveUrl("~") %>Public/css/Site.css" rel="stylesheet" type="text/css" />
    <link href="<%= ResolveUrl("~") %>Public/css/jqueryslidemenu.css" rel="stylesheet" type="text/css" />
    <!--[if lte IE 7]>
    <style type="text/css">
        html .jqueryslidemenu{height: 1%;} /*Holly Hack for IE7 and below*/
    </style>
    <![endif]-->
    <title>GRC</title>
</head>

<body>
    <div class="page">

        <div id="header">

            <div id="title">
                <h1>GRC</h1>
            </div>

            <div id="toolbarcontainer">
               <% Html.RenderAction<MenuController>(m => m.MainToolBar());%>
            </div> 

            <div id="menucontainer" class="jqueryslidemenu">
               <% Html.RenderAction<MenuController>(m => m.MainMenu());%>
            </div>

        </div>

        <div id="app-main">
            <% Html.RenderAction<HomeController>(c=>c.ShowHome()); %>
        </div>

        <div id="footer">
        </div>
    </div>

   <script type="text/javascript" src="../../Public/javascript/MicrosoftAjax.js"></script>   
   <script type="text/javascript" src="../../Public/javascript/MicrosoftMvcAjax.js"></script>
   <script type="text/javascript" src="../../Public/javascript/MicrosoftMvcValidation.js"></script>
   <script type="text/javascript" src="../../Public/javascript/jquery-1.4.1.min.js"></script>
   <script type="text/javascript" src="../../Public/javascript/jquery.validate.min.js"></script>
   <script type="text/javascript" src="../../Public/javascript/MicrosoftMvcJQueryValidation.js"></script>
   <script type="text/javascript" src="../../Public/javascript/jqueryslidemenu.js"></script>
   <script type="text/javascript" src="../../Public/javascript/GRC.js"></script>

</body>
</html>

我在html中生成了这个

<![CDATA[
3if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; }
4window.mvcClientValidationMetadata.push({"Fields":[{"FieldName":"Username","ReplaceValidationMessageContents":true,"ValidationMessageId":"Username_validationMessage","ValidationRules":[{"ErrorMessage":"Le nom d\u0027utilisateur est requis.","ValidationParameters":{},"ValidationType":"required"}]},{"FieldName":"Email","ReplaceValidationMessageContents":true,"ValidationMessageId":"Email_validationMessage","ValidationRules":[{"ErrorMessage":"L\u0027email est requis.","ValidationParameters":{},"ValidationType":"required"}]},{"FieldName":"Password","ReplaceValidationMessageContents":true,"ValidationMessageId":"Password_validationMessage","ValidationRules":[{"ErrorMessage":"Le mot de passe est requis.","ValidationParameters":{},"ValidationType":"required"}]},{"FieldName":"ConfirmPassword","ReplaceValidationMessageContents":true,"ValidationMessageId":"ConfirmPassword_validationMessage","ValidationRules":[{"ErrorMessage":"Le mot de passe est requis.","ValidationParameters":{},"ValidationType":"required"}]}],"FormId":"form0","ReplaceValidationSummary":false,"ValidationSummaryId":"validationSummary"});
5//]]> 

但是当我点击提交时,表单会被提交到服务器并且没有发生客户端验证吗?

我在这里失踪了什么? 感谢

1 个答案:

答案 0 :(得分:0)

看一下这个论坛主题:MVC 2 RC2 Clientside validation: Html.ValidationMessageFor() required even if using DataAnnotations?

基本上,您必须为要进行客户端验证的每个属性调用Html.ValidateFor(x => x.Property)Html.ValidationMessageFor(x => x.Property)

HTHS,
查尔斯