我正在使用MVC 4和JQuery Dialog进行表单验证。它的工作正常,但是当出现验证错误,如所需的验证错误时,它将单独显示部分视图,而不是在jquery ui对话框中。有人可以帮我解决这个问题吗?在此先感谢
答案 0 :(得分:0)
您需要一份表格:
@using (Html.BeginForm(...))
{
//Inside the form there would be a validation summary:
@Html.ValidationSummary( ... );
}
发布表单时,将根据您的模型验证字段:
public class MyModel
{
[Display( Name = "Description" )]
[StringLength( 256, ErrorMessage = "The description must not exceed 256 characters." )]
public override string Description { get; set; }
}
如果需要,可以通过拦截表单验证来自行处理jquery验证和演示。类似的东西:
$("form").validate({
onkeyup: false,
ignore: [],
rules: {
UserName: { required: true },
EmailAddress: { required: true },
}
},
groups: {
AllFields: "UserName EmailAddress"
},
messages: {
UserName: "Please fill in user name.",
EmailAddress: "Please fill in email address.",
},
errorPlacement: function ( error, element )
{
var container = $('#bb_validationSummary');
var list = container.find("ul");
if( list && list.length )
{
container.removeClass( "validation-summary-valid" );
container.addClass( "validation-summary-errors" );
$("<li />").html(error).appendTo(list);
}
}
} );
});