开发者

MVC 3 RC 2 - Check model state to prevent postback

开发者 https://www.devze.com 2023-02-01 15:15 出处:网络
I\'m doing a postback of a partial view with the code below.The controls in the partial view have mvc 3 rc 2 data annotations on them.When I submit the form with errors the errors show on the controls

I'm doing a postback of a partial view with the code below. The controls in the partial view have mvc 3 rc 2 data annotations on them. When I submit the form with errors the errors show on the controls but the form still gets submitted. Is there a way to check the model state on the client side so that 开发者_StackOverflowthe $.post in the function below can be stopped from been executed. Thanks.

$('#vehicleDetailsForm').submit(function () {

        $.post($(this).attr("action"), $(this).serialize(), function (result) {
            $('#vehicleDetailsPartialView').html(result);
        });
        return false; 
});


Are you using the jQuery validation plugin? If so, you can run the validation using:

$('#vehicleDetailsForm').submit(function () {
    if ($(this).valid()) {
        $.post($(this).attr("action"), $(this).serialize(), function (result) {
            $('#vehicleDetailsPartialView').html(result);
        });
    }
        return false; 
});

The same is probably true using the MS client validation library. I'll see if I can find an example.

0

精彩评论

暂无评论...
验证码 换一张
取 消