简体   繁体   English

您可以在asp.net的服务器端验证后运行自定义代码吗?

[英]Can you run custom code after server-side validation in asp.net?

I'm wondering if I can either return some values through a viewmodel (MVC) to the client, or execute some javascript on the client after validation results are known. 我想知道我是否可以通过视图模型(MVC)向客户端返回一些值,或者在知道验证结果之后在客户端上执行一些JavaScript。

Is anyone familiar with suitable classes and methods for doing this? 是否有人熟悉适合的类和方法?

Of course you can, since a normal AJAX request returns an answer. 当然可以,因为正常的AJAX请求会返回答案。 If you make this answer valid JSON and reassemble a client side model out of it, then validate it and run code accordingly. 如果使此答案有效为JSON并从中重组出客户端模型,则对其进行验证并相应地运行代码。

I usually solve this by adding a "success" and "payload" to my ajax replies from MVC. 我通常通过在MVC的ajax答复中添加“成功”和“有效载荷”来解决此问题。 All client side Ajax calls check for the "success" value, and decide to use the payload accordingly. 所有客户端Ajax调用都会检查“成功”值,并决定相应地使用有效负载。

for instance : 例如 :

reply is : 答复是:

{success : false, payload:"A null pointer occurred somehwere."}

or: 要么:

{success : true, payload:{id:12, label:"banana"}}

then i can do this in my ajax call : 那么我可以在我的ajax电话中做到这一点:

var yourViewmodel = {id:null, label:"banana"};

jQuery.ajax({
          cache: false,
          url: "./save",
          type: "POST",
          data: yourViewmodel,
          success: function (response) {
              if(response.success == true)
              {
                 if(!validate(response.payload))
                 {
                    //something about the response payload still was not valid!
                 }
                 else
                 {
                    //here we can do whatever we want to do if the response was valid.
                    onAfterValidate(response.payload);
                 }
              }
              else
              {
                 alert("An error occurred : " + response.payload);
              }
          },
          error: function(data, errorThrown)
          {
              alert('request failed :'+errorThrown);
          }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET客户端页面验证失败后,我可以运行一些自定义脚本吗? - Can I run some custom script after ASP.NET client side page validation fails? 你可以在 ASP.NET 上运行 javascript 服务器端吗? - Can you run javascript server side on ASP.NET? ASP.NET服务器端Javascript - ASP.NET Server-Side Javascript ASP.NET自定义验证器客户端和服务器端验证 - ASP.NET Custom Validator Client side & Server Side validation ASP.NET:客户端验证,服务器端操作 - ASP.NET: Client-side validation, server-side action 我可以通过ASP.NET将客户端javascript中的对象发送到服务器端代码吗? - Can I send an object from client-side javascript to server-side code via ASP.NET? 使用asp.net服务器端将现有的Javascript代码转换为多语言? - Convert existing Javascript code to multilingual using asp.net server-side? 将JavaScript代码与“服务器端ASP.Net AJAX”一起放在哪里? - Where to put javascript code with “Server-side ASP.Net AJAX”? 在ASP.NET MVC服务器端代码中动态创建和缩小JavaScript - Creating and minifying JavaScript dynamically in ASP.NET MVC server-side code ASP.NET - 如何使用 MVC 模式通过服务器端代码 (C#) 加载页面? - ASP.NET - How to load page via server-side code (C#) with MVC Pattern in place?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM