1
votes

I am working on MVC 4 with asp .net. All is well until i get the error saying

System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Data.Linq.DataQuery'1[FYPManagment.OfferedProject]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable'1[FYPManagment.Models.SupervisorModel]'.

I have search alot but still not working properly

My supervisorModel is:

public class SupervisorModel
{
    public OfferedProject offerproject { get; set; }
}

My supervisorController is:

public class SupervisorController : Controller
{
    DataContextDataContext dc = new DataContextDataContext();
    public ActionResult ViewProject()
    {
        var projectdata = (from s in dc.OfferedProjects
                           where s.FK_sup_ID == 2
                           select s);
        return View(projectdata);
    }
}

My View is:

@using FYPManagment;
@{
    List<OfferedProject> projectdata = (List<OfferedProject>)ViewData["projectdata"];
    Layout = null;
}
@model IEnumerable<FYPManagment.Models.SupervisorModel>
<!DOCTYPE html>
 AND
@foreach (var item in Model)
{
    <tr>
        <td>@item.offerproject.proj_title</td>
        <td>@item.offerproject.proj_description</td>
        <td><a href="#">Send Request</a></td>     
    </tr>
}
1
Please note that the model-view-controller tag is for questions about the pattern. There is a specific tag for the ASP.NET-MVC implementation.user3559349

1 Answers

1
votes

Currently, the variable projectdata is still a query expression of type IQueryable. It hasn't been evaluated/executed yet. The type of projectdata is different than the type your view is strongly typed to (IEnumerable<SupervisorModel>). That is the reason you are getting the type mismatch error.

First, update your action method to return the result of executing the query expression. You can call the ToList() method on the query expression.

public ActionResult ViewProject()
{
   var projectdata = (from s in dc.OfferedProjects
                           where s.FK_sup_ID == 2
                           select s).ToList();
    return View(projectdata);
}

Now make sure that your view is strongly typed to the same type you are sending from the action method:

@model IEnumerable<FYPManagment.Models.OfferedProject>
<table>
@foreach (var item in Model)
{
   <tr>
      <td>@item.offerproject.proj_title</td>
      <td>@item.offerproject.proj_description</td>
   </tr>
}
</table>