I want to build view using html/javascript/jquery and do not want to use razor syntax. How do I bind my model ? How do I iterate list in view to display rows?
Controller
- public ActionResult Index()
- {
- List<Employee> employeeList = new List<Employee>();
- string CS = ConfigurationManager.ConnectionStrings["mvcappDbConnection"].ConnectionString;
- using (SqlConnection con = new SqlConnection(CS))
- {
- SqlCommand cmd = new SqlCommand("SELECT * FROM Employee", con);
- cmd.CommandType = CommandType.Text;
- con.Open();
- SqlDataReader rdr = cmd.ExecuteReader();
- while (rdr.Read())
- {
- var employee = new Employee();
- employee.EmployeeID = Convert.ToInt32(rdr["EmployeeID"]);
- employee.LastName = rdr["LastName"].ToString();
- employee.FirstName = rdr["FirstName"].ToString();
- employeeList.Add(employee);
- }
- }
- return View(employeeList);
View:
- @{
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
- @model IEnumerable<aspnetmvc1.Models.Employee>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Employee list</title>
- <script>
- var Employees = @Html.Raw(Json.Encode(@Model));
- alert(Employees);
- for (var i = 0; i < Employees.length; i++) {
- alert(Employees[i].Lastname);
- alert(Employees[i].Firstname);
- }
- </script>
- </head>
- <body>
- <table style="width:50%">
- <tr>
- <th>Empployee ID</th>
- <th>Lastname</th>
- <th>Firstname</th>
- </tr>
- @*@foreach (var e in Model)
- {
- <tr>
- <td>@e.EmployeeID</td>
- <td>@e.LastName</td>
- <td>@e.FirstName</td>
- </tr>
- }*@
- </table>
- </body>
- </html>