MVC 3 jQuery 对话框上的客户端验证
我使用 jquery 对话框显示了很多表单,并且我希望在其上添加客户端验证。我读了一些例子,说 mvc 3 已经以某种方式支持 jquery 客户端验证,但我尝试包含必要的脚本,我的表单如下所示:
@using (Html.BeginForm("CreateFood", "Home", FormMethod.Post, new { id = "formData" }))
{
@Html.ValidationSummary(false, "Please fix these errors.")
当我尝试提交表单而不填写必填字段时,我仍然不喜欢收到任何消息。谁能给我更多关于这个的想法/解释/例子?
这里真的需要帮助...谢谢...
更新(在我的对话框的脚本中添加)
$createdialog.dialog("option", "buttons", {
"Cancel": function () {
//alert('Cancel');
$createdialog.dialog('close');
},
"Submit": function () {
var frm = $('#formData');
$.ajax({
url: '/Food/CreateFood',
type: 'POST',
data: frm.serialize(),
success: $createdialog.dialog('close')
});
}
});
一旦删除,打开对话框:
// Once drop, open dialog to create food
options.drop = function (event, ui) {
// Get the ContainerImgName which food dropped at
var cimg = $(this).attr('id');
// Pass in ContainerImgName to retrieve respective ContainerID
// Once success, set the container hidden field value in the FoodForm
$.ajax({
url: '/food/getcontainerid',
type: 'GET',
data: { cImg: cimg },
success: function (result) { $('#containerID').val(result); }
});
clear();
$.validator.unobtrusive.parse($createdialog);
$createdialog.dialog('open');
};
I am showing lots of form using jquery dialog and I wish to add in client side validation on it. I read through some examples, saying that mvc 3 already somehow support jquery client side validation, but I tried by including the necessary script, and my form like this:
@using (Html.BeginForm("CreateFood", "Home", FormMethod.Post, new { id = "formData" }))
{
@Html.ValidationSummary(false, "Please fix these errors.")
When i try to submit my form without fill in the required field, I still dint get any message. Can anyone give me more idea / explanation / examples on this??
Really needs help here... Thanks...
UPDATE (add in the script for my dialog)
$createdialog.dialog("option", "buttons", {
"Cancel": function () {
//alert('Cancel');
$createdialog.dialog('close');
},
"Submit": function () {
var frm = $('#formData');
$.ajax({
url: '/Food/CreateFood',
type: 'POST',
data: frm.serialize(),
success: $createdialog.dialog('close')
});
}
});
Once dropped, open dialog:
// Once drop, open dialog to create food
options.drop = function (event, ui) {
// Get the ContainerImgName which food dropped at
var cimg = $(this).attr('id');
// Pass in ContainerImgName to retrieve respective ContainerID
// Once success, set the container hidden field value in the FoodForm
$.ajax({
url: '/food/getcontainerid',
type: 'GET',
data: { cImg: cimg },
success: function (result) { $('#containerID').val(result); }
});
clear();
$.validator.unobtrusive.parse($createdialog);
$createdialog.dialog('open');
};
我遇到了同样的问题,解决方法:
当然,您可以在对话框的关闭事件上添加验证,具体取决于您在做什么,在我的情况下,弹出窗口只是用于显示错误,因此我执行了验证关于内容的加载。 (此弹出窗口显示的是操作结果)
I've faced the same problem, solved with:
of course you can add the validation on the close event of the dialog, depends on what you're doing, in my case the popup was just for displaying errors so I've performed validation on load of the content. (this pop up is displaying am Action result)