jQuery 客户端验证不适用于 MVC 3 MvcContrib.FluentHtml

发布于 2024-10-17 21:04:04 字数 2902 浏览 4 评论 0原文

使用 MVC 3 RTM 和 MvcContrib/FluentHtml 版本 3.0.51.0,我无法让 jQuery 客户端验证工作。服务器端验证工作正常,并返回显示正确的验证摘要等。但是,当表单发布应该在客户端停止以显示验证错误时,它总是尝试点击服务器端发布控制器操作。

对我所缺少的有什么想法吗?最新版本的 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>

View 页面继承自 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; }

Using MVC 3 RTM and MvcContrib/FluentHtml version 3.0.51.0, I can't get the jQuery client side validation to work. Server side validation works fine, and returns showing the correct validation summary, etc. But the form post always tries to hit the server-side post controller action when it should have stopped on the client side to display the validation error.

Any ideas of what I'm missing? Could the latest version of MvcContrib/FluentHtml be incompatible with jQuery client validation?

Here's my code:

Web.config has:

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

Site.Master page has:

<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>

View page inherits from MvcContrib's ModelViewPage:
Here's the view page:

<%@ 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>

Here's the controller post action:

[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)
            {

Here's the entity object with the Required field:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半步萧音过轻尘 2024-10-24 21:04:04

我发现,这肯定是 MvcContrib 搞砸了。我最初认为MvcContrib不是一个因素,因为当我从MvcContrib的ModelViewPage更改为Mvc.ViewPage时,我将“this.TextBox”切换为“Html.TextBox”,它仍然不起作用。但是,我刚刚发现我需要将控件切换到“Html.EditorFor”而不是“Html.TextBox”。

我在 google 上搜索了几分钟,看起来 MvcContrib 版本 3.0.51.0 不支持使用 EditorFor 的任何等效项进行 jquery 客户端验证,因此我必须将其撕掉并使用本机 MVC 控件。

您可以在此处查看一篇文章,其中包含我的方法的完整解决方案:使用-asp-net-mvc-and-jquery自动验证和格式化数据

I figured it out, it's definitely MvcContrib messing it up. I originally thought MvcContrib wasn't a factor because when I had changed from MvcContrib's ModelViewPage to Mvc.ViewPage, I switched the "this.TextBox" to "Html.TextBox", it still didn't work. However, I just discovered that I need to switch the control to "Html.EditorFor" instead of "Html.TextBox".

I've googled for a few minutes and it doesnt look like MvcContrib version 3.0.51.0 supports jquery client-side validation with any equivalent of EditorFor, so I'll have to just rip it out and use the native MVC controls.

You can see an article with a complete solution of my approach here: automatically-validate-and-format-data-with-asp-net-mvc-and-jquery

戏舞 2024-10-24 21:04:04

刚刚遇到这个问题,看起来像 斯科特·柯克兰有一个解决方案

Just ran headlong into this problem, looks like Scott Kirkland has a solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文