title: How To access and display the data from database using ajax jquery asp.net mvc
body: <p>I Am trying to fetch the data from database and display it in the page using ajax and jquery. Am new to this platform so can anyone help me
  Model:</p>
  
  <pre><code> public class EmployeeModel
   {
      public int EmpId { get; set; }
  
      public string EmpName { get; set; }
  
      public int Age { get; set; }
  
      public int Salary { get; set; }
  
   }
  </code></pre>
  
  <p>Controller :</p>
  
  <pre><code> private static readonly string connectionString =    ConfigurationManager.ConnectionStrings["ConnStringDb1"].ConnectionString;
      public ActionResult GetUser()
      {
          return View();
      }
  
      public JsonResult GetAllUser(int EmpId)
      {
          List&lt;EmployeeModel&gt; employee = new List&lt;EmployeeModel&gt;();
          string query = string.Format("Select * From Employee", EmpId);
          SqlConnection connection = new SqlConnection(connectionString);
          {
              using (SqlCommand cmd = new SqlCommand(query, connection))
              {
                  connection.Open();
                  SqlDataReader reader = cmd.ExecuteReader();
  
                  while (reader.Read())
                  {
                      employee.Add(
                          new EmployeeModel
                          {
                              EmpId = int.Parse(reader["EmpId"].ToString()),
                              EmpName = reader.GetValue(0).ToString(),
                              Age = int.Parse(reader["Age"].ToString()),
                              Salary = int.Parse(reader["Salary"].ToString())
                          }
                      );
                  }
              }
              return Json(employee, JsonRequestBehavior.AllowGet);
          }
      }
  </code></pre>
  
  <p>ajax: </p>
  
  <pre><code>      @{
               ViewBag.Title = "Home Page";
               var EmployeeModel =                       (List&lt;second_day.Models.EmployeeModel&gt;)Model;
         }
       &lt;div id="id"&gt;&lt;/div&gt;
       &lt;div id="firstName"&gt;&lt;/div&gt;
  &lt;div id="lastName"&gt;&lt;/div&gt;
  &lt;p id="getEmployee"&gt;Get Employee&lt;/p&gt;
  &lt;script src="~/Scripts/jquery-1.10.2.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  $(document).ready(function () {
      $('p#getEmployee').click(function () {
          GetEmployeeUsingAjax();
      });
  });
  
   function GetEmployeeUsingAjax() {
      $.ajax({
          type: 'GET',
          url: '@Url.Action("GetAllUser")',
          data:{"EmpId":EmpId},
          dataType: 'json',
          success: function (data) {
              console.log(data);
              //$('#id').text(emp.employee.Id);
              //$('#firstName').text(emp.employee.FirstName);
              //$('#lastName').text(emp.employee.LastName);
          },
          error: function (emp) {
              alert('error');
          }
      });
  }
  </code></pre>
  
  <p>
  Here i Need to fetch the data when its success else through error if it doesnt</p>
  
  <p>Am new to this platform can anyone help me</p>