Baji
Baji

Reputation: 131

Can't read values of json data received from controller in a view

I have json data that I am passing from controller and received using ajax get in view.I am unable to read the values in view. Please help. I am also attaching view code and controller code.

View:-

$(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));
            }

        })


    });


});

Controller:-

 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);



        }

Received data

Upvotes: 1

Views: 515

Answers (2)

Mohammad Olfatmiri
Mohammad Olfatmiri

Reputation: 1675

you can simply use this method:

success: function (data) {
     $.each(rows, function(){
          alert(this.FirstName +' ' +this.LastName )
     })

Upvotes: 0

Ibrahim Khan
Ibrahim Khan

Reputation: 20740

There is an array named rows in your json object which holds your data. Your success callback should be like following.

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)
     //})
}

Upvotes: 1

Related Questions