Jquery Validate 和 Asp.net MVC 之间的冲突

发布于 2024-09-03 05:40:14 字数 1409 浏览 2 评论 0原文

我正在使用 asp.net mvc 2.0 和 jquery validate 1.7 (http://bassistance. de/jquery-plugins/jquery-plugin-validation/)

发生的事情是这样的。用户可以单击链接来编辑产品。当用户单击他们想要编辑的产品时,会出现一个 jquery 对话框,其中包含文本框和下拉列表的形式。

这些 html 控件中填充了信息。假设如果用户选择编辑 Ipad 产品,将出现一个对话框,其中一个表单文本框将包含“Ipad”。

现在,该表单在服务器端呈现(表单处于部分视图中)。加载对话框时,会发出 ajax 请求来获取该部分视图,在 ajax 调用的响应部分中,我会执行类似的操作,

$('#EditDialog).html(ajaxresponse).dialog({...});

所以我会在对话框中呈现类似的内容,

<form id="EditProduct">
Product Name: <input type="text"  value="IPad" name="ProductName" />
</form> 

现在我的 jquery 验证将是这样的。

 $("#EditProduct").validate(
           {
               errorContainer: "#Errorbox",
               errorLabelContainer: "#Errorbox ul",
               wrapper: "li",
               rules:
               {
                   ProductName: "required"
               }
           });

所以我知道这是有效的,因为我使用相同的验证来添加产品,如果您尝试将 ProductName 留空,它将显示验证错误。

现在它不适用于编辑,我想我知道原因,但不知道如何修复它。

文本框的值为“iPad”,这就是 Html.TextBoxFor() 呈现它的方式。但是,如果用户将产品名称更改为“Iphone”或空白,则该值永远不会改变。 html 中始终为“Ipad”。

所以我认为当验证进行时,看起来它已经存在了一个值。它是有效的,尽管实际上它可能是空白的。

当我通过 ajax 发布到服务器时,它会获得正确的值,并且服务器端验证会停止它,但由于这个问题,我的整个客户端验证变得毫无用处,因为它永远不会更改 html。

I am using asp.net mvc 2.0 and jquery validate 1.7 (http://bassistance.de/jquery-plugins/jquery-plugin-validation/)

What happens is this. A user can click on a link to edit a product. When a user clicks on which product they want to edit a jquery dialog box appears with a form of textboxes and dropdown lists.

These html controls have information filled in them. Say if the user chooses to edit the Ipad product a dialog will appear and one of the form textboxes will have "Ipad" in it.

Now this form gets rendered on the server side(the form is in a partial view). When loading the dialog box a ajax request is made to get that partial view and in the response part of the ajax call I do something like

$('#EditDialog).html(ajaxresponse).dialog({...});

So I would have something like this rendered in my dialog box

<form id="EditProduct">
Product Name: <input type="text"  value="IPad" name="ProductName" />
</form> 

Now my jquery validate would be something like this.

 $("#EditProduct").validate(
           {
               errorContainer: "#Errorbox",
               errorLabelContainer: "#Errorbox ul",
               wrapper: "li",
               rules:
               {
                   ProductName: "required"
               }
           });

So I know this works because I use the same validate for add product and if you try to leave ProductName blank it will show a validation error.

Now it does not work with the edit one though and I think I know the reason why but not how to fix it.

The value for the textbox is "IPad" this is how the Html.TextBoxFor() renders it. However if a user goes and changes the product name to "Iphone" or blank the value never changes. It is always "Ipad" in the html.

So I think when the validate goes and looks it goes oh there is a value already in it. It is valid even though in reality it might be blank.

When I post to the server through ajax it gets the right value and the server side validation stops it but my entire clientside validation is rendered useless because of this problem as it will never change the html.

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

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

发布评论

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

评论(1

享受孤独 2024-09-10 05:40:14

这似乎不太对劲!

想象一下,在“add”情况下,您呈现的 HTML 看起来像这样:

<form id="AddProduct">
Product Name: <input type="text"  value="" name="ProductName" />
</form>

因此“HTML”有一个空白值,但验证插件可以正确判断 ProductName 是否已填充?

我不太确定问题是什么,但如果您有调试器(即 Firebug),请尝试在“检查”功能内的验证插件中放置一个中断(~1.6 中的第 475 行)。您应该能够观察 element.value 以查看它是否已找出正确的用户编辑值。

This doesn't seem quite right!

Imagine in the "add" case, the HTML you render looks something like this:

<form id="AddProduct">
Product Name: <input type="text"  value="" name="ProductName" />
</form>

So the "HTML" has a blank value, yet the validation plugin can correctly tell once ProductName has been filled?

I'm not exactly sure what the issue is, but if you have a debugger (i.e. Firebug), try putting a break in the validation plugin, within the "check" function (~line 475 in 1.6). You should be able watch element.value to see if it has figured out the correct user-edited value.

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