Im experiencing an error on trying to fetch data from a db using a list scaffolding. The error and the code is as follow.
My Model:
namespace ShimbaSchool.Models
{
[Table("tblStaff")]
public class Staff
{
[Key]
public int StaffId { get; set; }
[Required]
[DisplayName("Upload Image")]
public string ImagePath { get; set; }
[Required,MinLength(2),DisplayName("Staff Name")]
public string StaffName { get; set; }
[Required,MaxLength(250)]
[DisplayName("Teacher's Subject")]
public string StaffSpecialty { get; set; }
[NotMapped]
public HttpPostedFileBase ImageFile { get; set; }
}
}
My Controller:
namespace ShimbaSchool.Controllers
{
public class StaffController : Controller
{
EventMessageDepartmentContext db = new EventMessageDepartmentContext();
public ActionResult Index()
{
return View(db.StaffTable.ToList());
}
}
}
The View:
@model IEnumerable<ShimbaSchool.Models.Staff>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_MyLayout.cshtml";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.ImagePath)
</th>
<th>
@Html.DisplayNameFor(model => model.StaffName)
</th>
<th>
@Html.DisplayNameFor(model => model.StaffSpecialty)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ImagePath)
</td>
<td>
@Html.DisplayFor(modelItem => item.StaffName)
</td>
<td>
@Html.DisplayFor(modelItem => item.StaffSpecialty)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.StaffId }) |
@Html.ActionLink("Details", "Details", new { id=item.StaffId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.StaffId })
</td>
</tr>
}
</table>
On executing i get the error:
The model item passed into the dictionary is of type 'System.Collections.Generic.List 1[ShimbaSchool.Models.Staff]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1[ShimbaSchool.Models.EventMessageDepartment]'.
Please help me fix this situation and understand the logic so that there is no next time. To add on i used the same model with another controller and its working fine though the view were rendered on different layouts of the same project.