HI everyone
i am new to mvc i am facing a problem, i have a simple view that contains a form and a foreach loop the syntax of the loop goes fine if i did this
@model IEnumerable<SearchBox.Models.Students>
but the syntax of labelfor give me error and if i did this
@model SearchBox.Models.Students
to my view then the foreach loop Model is underlined with a red line
What i will hhave to do to resolve my problem kindly help me to get out of it thankx in advance
What I have tried:
Here is View
@model IEnumerable<SearchBox.Models.Students>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.Name, "Name", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-8">
@Html.EditorFor(model => model.Name, new { htmlattributes = new { @class = "form-control" } })
</div>
</div>
</div>
}
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
</table>
Controller
public class DefaultController : Controller
{
private static List<Students> students = new List<Students>()
{
new Students() {Id = 1, Name = "Student - 1"},
new Students() {Id = 2, Name = "Student - 2"},
new Students() {Id = 3, Name = "Student - 3"},
new Students() {Id = 4, Name = "Student - 4"},
new Students() {Id = 5, Name = "Student - 5"},
new Students() {Id = 6, Name = "Student - 6"},
new Students() {Id = 7, Name = "Student - 7"},
new Students() {Id = 8, Name = "Student - 8"},
new Students() {Id = 9, Name = "Student - 9"}
};
public ActionResult Index()
{
return View(students);
}
}