我有json数据,我从控制器传递并使用ajax get在视图中接收。我无法读取视图中的值。请帮忙。我还附加了视图代码和控制器代码。
$(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);
}