Hi mukesh1,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of NorthWind database that you can download using the link given below.
Download Northwind Database
As per the example for EmployeeId 10 there is no record in database. So i added a default FirstName and Last Name to the Employee Entity.
So change thye code as per your requirement.
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
NorthwindEntities db = new NorthwindEntities();
List<Employee> employees = new List<Employee>();
for (var i = 10; i >= 5; i--)
{
var m = db.Employees.Where(s => s.EmployeeID == i).FirstOrDefault();
if (m == null)
{
employees.Add(new Employee { FirstName = "John Hammond", LastName = "United States" });
}
else
{
employees.Add(new Employee { FirstName = m.FirstName, LastName = m.LastName });
}
}
return View(employees);
}
}
View
@model IEnumerable<_413718_Add_List_ForLoop.Employee>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<table class="table">
<tr>
<th>@Html.DisplayNameFor(model => model.LastName)</th>
<th>@Html.DisplayNameFor(model => model.FirstName)</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.LastName)</td>
<td>@Html.DisplayFor(modelItem => item.FirstName)</td>
</tr>
}
</table>
</body>
</html>
Screenshot