【发布时间】:2018-11-14 18:19:57
【问题描述】:
我需要将下面的 SQL 结果传递给 ASP.Net 中的 Javascript 吗?我试图在 JS 中声明这两个字段,但不能正确。如何在 JS 中得到结果?
var Description = "<%=this.Description%>"
var ApplicationSourceCount = "<%=this.ApplicationSourceCount%>"
用 C# 声明字符串
public class ApplicantSourceData
{
public string Description { get; set; }
public string ApplicantSourceCount { get; set; }
}
C# WebMethod
[WebMethod]
public List<ApplicantSourceData> GetApplicantSourceData(List<string> aData)
{
//SqlDataReader reader;
List<ApplicantSourceData> GetApplicantSourceData = new List<ApplicantSourceData>();
string connectionString = ConfigurationManager.ConnectionStrings["ATL2"].ConnectionString;
string commandTextApplicantsByMonthCount = Properties.Queries.commandTextApplicantsByMonthCount;
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(commandTextApplicantsByMonthCount))
{
command.CommandText = commandTextApplicantsByMonthCount;
command.CommandType = CommandType.Text;
command.Connection = con;
con.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
int counter = 0;
while (reader.Read())
{
ApplicantSourceData tsData = new ApplicantSourceData();
tsData.Description = reader["Description"].ToString();
tsData.ApplicantSourceCount = reader["ApplicantSourceCount"].ToString();
GetApplicantSourceData.Add(tsData);
counter++;
}
}
}
return GetApplicantSourceData;
}
}
我尝试了以下方法,但它没有
【问题讨论】:
-
如果您从 javascript 调用 web 方法,则将结果作为 JSON 字符串返回,然后在客户端进行解析。
-
我该怎么做?
-
使用 Newtonsoft.Json 或其他库将 c# 对象转换为 json。参考这个stackoverflow.com/a/42649928/795683
标签: javascript c# asp.net webmethod