无法读取从视图中的控制器接收的 json 数据的值
Can't read values of json data received from controller in a view
我有 json 从控制器传递并使用 ajax 接收的数据 view.I 无法读取视图中的值。请帮忙。我还附上了视图代码和控制器代码。
查看:-
$(function () {
$('#btn1').click(function () {
alert('hello');
$.ajax({
url: '/TodoList/Check',
type: 'Get',
dataType: 'json',
//contentType: "application/json; charset=utf-8",
success: function (data) {
// fs = data[1].FirstName;
alert(JSON.stringify(data.FirstName));
}
})
});
});
控制器:-
public JsonResult Check()
{
string ConnectionString = ConfigurationManager.ConnectionStrings["EmployeeContext"].ConnectionString;
SqlDataReader rdr = null;
List<Employee> l = new List<Employee>();
Employee e = new Employee();
int Sal = e.Salary;
SqlDataAdapter da;
DataSet ds = new DataSet();
using (SqlConnection connection = new SqlConnection("data source=.; database=Srivatsava; integrated security=SSPI"))
{
connection.Open();
da = new SqlDataAdapter("select UserName,Pass from UserPass", connection);
da.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
l.Add(new Employee() { FirstName = dr[0].ToString(), LastName = dr[1].ToString() });
}
connection.Close();
}
//int pageIndex = Convert.ToInt32(page) - 1;
//int pageSize = rows;
var todoListsResults = l.Select(
a => new
{
a.FirstName,
a.LastName
});
// int totalRecords = todoListsResults.Count();
//var totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows);
//if (sord.ToUpper() == "DESC")
//{
// todoListsResults = todoListsResults.OrderByDescending(s => s.FirstName);
//todoListsResults = todoListsResults.Skip(pageIndex * pageSize).Take(pageSize);
//}
//else
//{
// todoListsResults = todoListsResults.OrderBy(s => s.FirstName);
//todoListsResults = todoListsResults.Skip(pageIndex * pageSize).Take(pageSize);
// }
var jsonData = new
{
// total = totalPages,
// page,
//records = totalRecords,
rows = todoListsResults
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
您的 json
对象中有一个名为 rows
的数组,用于保存您的数据。您的 success
回调应该如下所示。
success: function (data) {
var rows=data.rows;
alert(rows[0].FirstName);
//use each loop to go through all data
//$.each(rows, function(){
//alert(this.FirstName)
//})
}
你可以简单地使用这个方法:
success: function (data) {
$.each(rows, function(){
alert(this.FirstName +' ' +this.LastName )
})
我有 json 从控制器传递并使用 ajax 接收的数据 view.I 无法读取视图中的值。请帮忙。我还附上了视图代码和控制器代码。
查看:-
$(function () {
$('#btn1').click(function () {
alert('hello');
$.ajax({
url: '/TodoList/Check',
type: 'Get',
dataType: 'json',
//contentType: "application/json; charset=utf-8",
success: function (data) {
// fs = data[1].FirstName;
alert(JSON.stringify(data.FirstName));
}
})
});
});
控制器:-
public JsonResult Check()
{
string ConnectionString = ConfigurationManager.ConnectionStrings["EmployeeContext"].ConnectionString;
SqlDataReader rdr = null;
List<Employee> l = new List<Employee>();
Employee e = new Employee();
int Sal = e.Salary;
SqlDataAdapter da;
DataSet ds = new DataSet();
using (SqlConnection connection = new SqlConnection("data source=.; database=Srivatsava; integrated security=SSPI"))
{
connection.Open();
da = new SqlDataAdapter("select UserName,Pass from UserPass", connection);
da.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
l.Add(new Employee() { FirstName = dr[0].ToString(), LastName = dr[1].ToString() });
}
connection.Close();
}
//int pageIndex = Convert.ToInt32(page) - 1;
//int pageSize = rows;
var todoListsResults = l.Select(
a => new
{
a.FirstName,
a.LastName
});
// int totalRecords = todoListsResults.Count();
//var totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows);
//if (sord.ToUpper() == "DESC")
//{
// todoListsResults = todoListsResults.OrderByDescending(s => s.FirstName);
//todoListsResults = todoListsResults.Skip(pageIndex * pageSize).Take(pageSize);
//}
//else
//{
// todoListsResults = todoListsResults.OrderBy(s => s.FirstName);
//todoListsResults = todoListsResults.Skip(pageIndex * pageSize).Take(pageSize);
// }
var jsonData = new
{
// total = totalPages,
// page,
//records = totalRecords,
rows = todoListsResults
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
您的 json
对象中有一个名为 rows
的数组,用于保存您的数据。您的 success
回调应该如下所示。
success: function (data) {
var rows=data.rows;
alert(rows[0].FirstName);
//use each loop to go through all data
//$.each(rows, function(){
//alert(this.FirstName)
//})
}
你可以简单地使用这个方法:
success: function (data) {
$.each(rows, function(){
alert(this.FirstName +' ' +this.LastName )
})