加载局部视图时,jquery对话框中的MVC检查验证客户端

时间:2012-04-03 11:37:15

标签: jquery asp.net-mvc asp.net-mvc-3 razor

在MVC中的

我用jquery对话框显示部分视图:

    @Html.Raw(@Ajax.ActionLink("replaceText", "AddToMembers", new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "result", InsertionMode = InsertionMode.Replace, OnSuccess = "openPopup" }).ToString().Replace("replaceText" , "<img src='images/btn_12.jpg' width='205' height='72' border='0'>"))

(AddToMemer返回部分视图)

我的成员模型类是这样的:

       public class Members
        {
            public int id { get; set; }
            [Required(ErrorMessage=" *",AllowEmptyStrings=false)]

            public string name { get; set; }
            [Required(ErrorMessage = "*", AllowEmptyStrings = false)]
            [RegularExpression(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", ErrorMessage = "*")]

            public string email { get; set; }


            public bool isValid { get; set; }

            public DateTime date { get; set; }

        }

这是我的部分观点:(csFile)

@model MVC.Models.Members
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"type="text/javascript"> </script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")"type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(false)
    <fieldset>
        <legend >register</legend>
        <div style="font-family: Tahoma; font-size: 12px;">
        <div class="editor-label">
            @Html.LabelFor(model => model.name)
              <br />
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.name)
            <br />
            @Html.ValidationMessageFor(model => model.name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.email)
              <br />
            @Html.ValidationMessageFor(model => model.email)
        </div>

        <p align=center>
            <input id="SubmitButton" type="submit" class="normalText" value="send" />
        </p>
        </div>
    </fieldset>


}

但是当对话框加载并且我点击发送按钮时,验证不会在客户端检查,我会重定向到新页面并在那里检查验证。 有什么问题?

1 个答案:

答案 0 :(得分:0)

我是prsumiong,因为你已经标记了这个MVC3,你想使用不引人注目的javascript验证,在这种情况下你需要按照这里的步骤进行:

http://davidhayden.com/blog/dave/archive/2010/12/30/UnobtrusiveJavaScriptASPNETMVC3AjaxHelpers.aspx