如何在Ajax调用上对服务器发送的值进行服务器端验证 [英] How to make Server side validation on ajax call for values sent at controller

查看:50
本文介绍了如何在Ajax调用上对服务器发送的值进行服务器端验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

服务器端验证通过ajax调用发送的值.虽然我已经应用了一些客户端jquery验证但是,如果有人阻止了客户端javascript,那么它将失败.我想知道如何对发送到控制器的Ajax发布值应用服务器端验证,并可以通知客户端.

Server side validation for values sent through ajax call. Although i have applied some client side jquery validation but if somebody blocks client side javascript then it will fail. I want to know how can i apply server side validation for ajax post values sent to a controller and can notify to client side.

我的案件在ajax帖子上,我正在从订购网站发送一些值.像数量,价格,项目大小一样,我想知道如何在控制器上验证这些值.

My case is on ajax post i am sending some values taking from a order site. Like quanity, price, itemsize i want to know how can i validate these values at the controller.

var CustomCategory = {
    OnCustomClick: function (productID, productOptionID, itmPrice, qty) {
        $.ajax({
            url: 'Customization/GetProductCustomization?pId=' + productID + '&pOptionID=' + productOptionID + '&itmPrice=' + itmPrice + '&qty=' + qty,
            //url: 'Customization/vwGetProductCustomization',
            type: 'POST',
        }).success(function (data) {
            //alert(data);
            $("#myModal").html(data);
        });
    }
}

就像某人为Java脚本取消了数量数字功能并发送了一些错误的值.

Like somebody makes quantity numeric functionality off for java script and send some wrong values.

推荐答案

您应该首先实现服务器端模型验证,然后再扩展到jquery非侵入式验证.默认情况下对此有很多支持,并且仅在需要创建自己的自定义验证时才需要做一些实际的工作.要创建自定义验证器,您可以查看此答案,并阅读提供的链接以获取更多信息.

You should start by implementing serverside model validation, and then expand to jquery unobtrusive validation. There are a bunch of support for this by default, and you're only going to do some actual hard work if you need to create your own custom validation. To create a custom validator you can look at this answer and also read the provided link to get some more information.

要创建som标准模型验证,您只需将其添加到您的 ViewModel

To create som standard model validation you can just add them in your ViewModel

public class Movie {
public int ID { get; set; }

[Required]
public string Title { get; set; }

[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }

[Required]
public string Genre { get; set; }

[Range(1, 100)]
[DataType(DataType.Currency)]
public decimal Price { get; set; }

[StringLength(5)]
public string Rating { get; set; }
}

您需要导入 System.ComponentModel.DataAnnotations 命名空间,才能使用此 Attributes .完成之后,只需包含 jquery.unobtrusive 脚本,然后在使用ajax发布表单之前,只需检查表单是否有效,否则就可以返回false:

You need to import System.ComponentModel.DataAnnotations namespace to be able to use this Attributes. After that is done, just include jquery.unobtrusive script and before posting your form with ajax, just check if the form is valid, otherwise you can just return false:

if ($(form).valid()) {
    $.ajax({ .....
}
else {
    return false;
}

要确保没有漏电,您还需要在控制器中检查模型

To make sure that nothing slips through, you also need to check your model in your controller

public ContentResult CheckForm(SomeViewModel vm)
{
    if (ModelState.IsValid) {
    ...... //Do something
    }
    return vm;
}

如果ViewModel上的所有验证属性均有效,则 IsValid 属性将仅返回 true .

The IsValid property will only return true if all validation attributes on your ViewModel are valid.

这篇关于如何在Ajax调用上对服务器发送的值进行服务器端验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆