-1

This is a common Error on SO but I seem not to be able to fix the error The model item passed into the dictionary is of type 'System.String', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[ITEye.ViewModels.IssueViewModel]'. Any help to troubleshoot will be appreciated

Viewmodel

 public class IssueViewModel
    {
        public int ItemId { get; set; }
        public string Name { get; set; }
        public string ItemLocation { get; set; }
        public string UserName { get; set; }
    }

Controller

public ActionResult Issueds()
        {
            var query = Issue();
            return View(query);
        }
        public IEnumerable Issue()
        {
            var issued = from item in db.Items
                        join issues in db.Issues on item.ItemId equals issues.ItemId
                        join users in db.Staffs on issues.StaffId equals users.StaffId
                        where issues.StaffId == users.StaffId

                         select new IssueViewModel()
                        {
                            ItemId = item.ItemId,
                            Name = item.ItemName,
                            ItemLocation = item.Location.LocName,
                            UserName = users.StaffName
                        };

            return issued.AsEnumerable();
}

View

@model IEnumerable<ITEye.ViewModels.IssueViewModel>
@foreach (var item in Model) {

<p>
   @item.Name
</p>

}
Diin
  • 565
  • 11
  • 41
  • I put something together basically the same as your code and it ran fine. Are you sure there's nothing else changing the Model between the Controller and the View, like an ActionFilter or something? – Rhumborl Sep 24 '14 at 19:42
  • I wonder why this question has been down voted for no research effort. – Diin Sep 24 '14 at 23:42

3 Answers3

0

As sure as I was that the code was okay as I had researched wide on this, the problem turned out to be an error in the view. I used @{Html.RenderPartial("Issueds", "Issue");} instead of @{Html.RenderAction("Issueds", "Issue");}

Diin
  • 565
  • 11
  • 41
-1

Your Issue method should return an IEnumerable<ITEye.ViewModels.IssueViewModel> not the non-generic type IEnumerable

   public IEnumerable<ITEye.ViewModels.IssueViewModel> Issue()
    {
        var issued = from item in db.Items
                    join issues in db.Issues on item.ItemId equals issues.ItemId
                    join users in db.Staffs on issues.StaffId equals users.StaffId
                    where issues.StaffId == users.StaffId

                     select new IssueViewModel()
                    {
                        ItemId = item.ItemId,
                        Name = item.ItemName,
                        ItemLocation = item.Location.LocName,
                        UserName = users.StaffName
                    };

        return issued;
    }
Cam Bruce
  • 5,632
  • 19
  • 34
  • How does this become a string in the view? – Rhumborl Sep 24 '14 at 19:17
  • It was becoming a string in the view in the OP's code because just a non-generic IENumerable was being passed in. – Cam Bruce Sep 24 '14 at 19:23
  • Thanks the code works alright the culprit was using @{Html.RenderPartial("Issueds", "Issue");} instead of @{Html.RenderAction("Issueds", "Issue");} – Diin Sep 24 '14 at 23:38
-1

You have not materialised the Entity Framework query before you return it from Issue() so it will be passed to the view as a SQL string - AsEnumerable() will not do anything because it is already an IEnumerable. You need to materialise the query so use something like ToList() instead.

Also change the return type of the method to be generic:

public ActionResult Issueds()
{
    var query = Issue();
    return View(query);
}

public IEnumerable<IssueViewModel> Issue()
{
    var issued = from item in db.Items
                join issues in db.Issues on item.ItemId equals issues.ItemId
                join users in db.Staffs on issues.StaffId equals users.StaffId
                where issues.StaffId == users.StaffId

                select new IssueViewModel()
                {
                    ItemId = item.ItemId,
                    Name = item.ItemName,
                    ItemLocation = item.Location.LocName,
                    UserName = users.StaffName
                };

    return issued.ToList();
}
Rhumborl
  • 16,349
  • 4
  • 39
  • 45
  • 1
    No need to call `ToList` EF queries return an `IQueryable`, which implements `IENumerable` and won't return a SQL String. – Cam Bruce Sep 24 '14 at 19:12
  • Thanks the code works alright the culprit was using @{Html.RenderPartial("Issueds", "Issue");} instead of @{Html.RenderAction("Issueds", "Issue");} – Diin Sep 24 '14 at 23:41