TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Tanvi Jain
1.7k
53
6.7k
View without using razor syntax. How to bind model?
May 5 2021 1:19 PM
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>
Reply
Answers (
2
)
SQL Query to Linq
How to send Otp for Registered mobile number in MVC