如何在ASP.NET webform方法中将客户验证消息作为json返回 [英] How do I return custome validation message as json in ASP.NET webform method

查看:429
本文介绍了如何在ASP.NET webform方法中将客户验证消息作为json返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 catch (Exception ex)
            {
			 throw ex;
				
            }
            finally
            {
                con.Close();
            }


            ArrayList a = new ArrayList();
            a.Add(reportList);
            a.Add(p);
 if (reportList.Count()  != 0)
            {
 return JsonConvert.SerializeObject(a);
            }
			else{
			
			return // "custom message records not found"
}





我尝试过:





What I have tried:

 catch (Exception ex)
            {
			 throw ex;
				
            }
            finally
            {
                con.Close();
            }


            ArrayList a = new ArrayList();
            a.Add(reportList);
            a.Add(p);
 if (reportList.Count()  != 0)
            {
 return JsonConvert.SerializeObject(a);
            }
			else{
			
			return // "custom message records not found"
}

推荐答案

您可以创建对象,然后将其转换为json对象。示例。



You can create object and then convert it to json object. Example.

public class ValidationMessage
{
    public int success { get; set; }
    public string message { get; set; }
}
Usage:

var valMessage = "{\"success\":1,\"message\":\"successfully created.\"}";
return JsonConvert.DeserializeObject<ValidationMessage>(valMessage);


试试这个

只需比较第一个索引值javascript中的json数组



try this
just compare the first index value of the json array in javascript

if (reportList.Count()  != 0)
            {
            a.Add("success");
              a.Add(reportList);
            a.Add(p); 
            }
			else {
            a.Add("failure");
              a.Add("Your custom message");  
        }
             return JsonConvert.SerializeObject(a);


这篇关于如何在ASP.NET webform方法中将客户验证消息作为json返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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