1

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.

TanvirArjel
  • 30,049
  • 14
  • 78
  • 114
  • Actually the list you are passing from controller method to view is a `List of EventMessageDepartment`, not `List of Staff`. Please check it and pass `List of Staff` from controller method to the view. – TanvirArjel Feb 25 '19 at 06:22
  • Actually, the EventMessageDepartmentContext you see on the controller is my data context that enable communication to the db. – شعبن سعيد Feb 25 '19 at 06:33
  • Yes! but you have to select the table properly. Better give remote access with team viewer id and pass. I shall fix your problem. – TanvirArjel Feb 25 '19 at 06:43
  • 1 421 119 183 Cant you rectify from the code i posted> – شعبن سعيد Feb 25 '19 at 06:45
  • Possible duplicate of [The model item passed into the dictionary is of type .. but this dictionary requires a model item of type](https://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ) – Tetsuya Yamamoto Feb 25 '19 at 07:17
  • The duplicate link above already covers your issue - you're passing `db.StaffTable.ToList()` result to view which has model directive `IEnumerable`. This is a well-known problem which occurred when passing wrong type from controller to view. – Tetsuya Yamamoto Feb 25 '19 at 07:22

3 Answers3

3

Actually the list you are passing from controller method to view is a List of EventMessageDepartment, not List of Staff. Please check it properly and pass List of Staff from controller method to the view.

If you are ensured that the data you are passing to the view is List<ShimbaSchool.Models.Staff> then please check your Layout.cshtml page. May be there is @model IEnumerable<EventMessageDepartment> is referenced.

TanvirArjel
  • 30,049
  • 14
  • 78
  • 114
0

Did you try the following?

    public ActionResult Index()
    {
        return View(db.StaffTable.AsEnumerable());
    }
0

First you need to return an IEnumerable version of your model to the list view.

@model IEnumerable<IdentityManager.Models.MerchantDetail>

Second, you need to return a list from the database. I am doing it via SQL Server, so this is code I got working.

    public IActionResult Merchant_Boarding_List()
        List<MerchantDetail> merchList = new List<MerchantDetail>();
        var model = new MerchantDetail();

        try
        {
            using (var con = new SqlConnection(Common.DB_CONNECTION_STRING_BOARDING))
            {
                con.Open();
                using (var command = new SqlCommand("select * from MerchantDetail md where md.UserGUID = '" + UserGUID + "'", con))
                {
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            model.biz_dbaBusinessName = reader["biz_dbaBusinessName"].ToString();
                            merchList.Add(model);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
        }

        return View(merchList);