不会为使用 AJAX 获取的部分视图生成客户端验证脚本
我正在尝试使用 MicrosoftMvcJQueryValidation 设置客户端验证 处理 ajax 提交的表单。 如果直接从视图渲染部分视图,它的工作效果非常好。 但是,当我尝试通过 XHR 获取它时,例如在 JQuery 对话框中显示它, 不会为输出 html 生成客户端验证 javascript。 有什么想法吗?
工作代码 - 部分视图使用 Html.RenderPartial 呈现:
视图:
<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% Html.RenderPartial("New"); %>
</asp:Content>
部分视图:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Product>" %>
<% Html.EnableClientValidation();%>
<% Html.BeginForm();%>
<%= Html.EditField(m => m.price)%>
<%= Html.ValidationMessageFor(m => m.price)%>
<% Html.EndForm();%>
不工作代码 - 部分视图使用 JQuery load() 函数获取。
查看:
<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
....
$("#dialog").load('~/Product/New/');
$("#dialog").dialog("open");
....
<div id="dialog" title=""></div>
</asp:Content>
相关控制器操作:
public PartialViewResult New(int id)
{
return PartialView(service.GetProduct());
}
谢谢。
I am trying to setup client side validation using MicrosoftMvcJQueryValidation
to work with ajax submitted forms.
It works perfectly fine if the partial view is rendered directly from a view.
However when I try to fetch it over XHR, for example to show it in a JQuery Dialog,
client validation javascript is not generated for the output html.
Any ideas?
Working code - partial view is rendered using Html.RenderPartial:
View:
<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% Html.RenderPartial("New"); %>
</asp:Content>
Partial View:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Product>" %>
<% Html.EnableClientValidation();%>
<% Html.BeginForm();%>
<%= Html.EditField(m => m.price)%>
<%= Html.ValidationMessageFor(m => m.price)%>
<% Html.EndForm();%>
Not working code - partial view is fetched with JQuery load() function.
View:
<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
....
$("#dialog").load('~/Product/New/');
$("#dialog").dialog("open");
....
<div id="dialog" title=""></div>
</asp:Content>
Relevant controller action:
public PartialViewResult New(int id)
{
return PartialView(service.GetProduct());
}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
阅读 ARM 提到的博客后,解决方案似乎确实取决于
关于如何加载部分视图。
最终对我有用的是调用 __MVC_EnableClientValidation
将视图分配给元素后:
After reading the blog ARM has mentioned it seems the solution really depends
on how one is loading the partial view.
What eventually worked for me is to call __MVC_EnableClientValidation
after view has been assigned to an element:
对于所有返回通过 AJAX 插入 DOM 的 JavaScript 的部分视图,都会发生这种情况。问题在于,随分部视图返回的 JavaScript 在插入到文档中时不会被执行/解释。
不幸的是,因为你正在处理的是生成的 JavaScript,我可以解释为什么你会遇到这个问题,但我不知道解决方案。如果这是您编写的函数,我建议将其添加到 OnComplete.我所能提供的就是这是一个 JavaScript 限制,并开始从那里开始寻找。
顺便说一句:这看起来很有希望 http ://adammcraventech.wordpress.com/2010/06/11/asp-net-mvc2-ajax-executing-dynamically-loaded-javascript/
This will happen for all partial views which return JavaScript inserted into the DOM via AJAX. The problem is that the JavaScript returned with the partial view is not executed/interpreted when it is inserted into the document.
Unfortunately, because what you are dealing with is generated JavaScript I can explain why you are experiencing the problem but I'm at a loss for a solution. Were this a function you wrote I'd suggest adding it to the OnComplete. All I can offer is that this is a JavaScript limitation and to start looking there.
BTW: this looks promising http://adammcraventech.wordpress.com/2010/06/11/asp-net-mvc2-ajax-executing-dynamically-loaded-javascript/