MVC: The model item passed into the dictionary is

2019-05-17 09:02发布

问题:

I am using EF model first and using viewmodels in MVC, I have problems with this error:

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[NKI3.Models.Question]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[NKI3.ViewModels.IndexCreateViewModel]'.

This is my viewmodel:

public class IndexCreateViewModel
{
    public string QuestionText { get; set; }
    public string Sname { get; set; }
    public string Cname {get;set;}
}

This is my Action in my controller:

public ActionResult Index()
{
    var Q = db.Question.Include(a => a.SubjectType).Include(a => a.CoreValue);
    return View(Q.ToList());   
}

Here is my strongly typed view:

@model IEnumerable<NKI3.ViewModels.IndexCreateViewModel>

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            QuestionText
        </th>
        <th>
            Sname
        </th>
        <th>
            Cname
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.QuestionText)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Sname)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Cname)
        </td>

    </tr>
}

</table>

I dont seem to find the solution for this, do I have to return my viewmodel inside the controller action index? Any help is appreciated.

Thanks in advance!

Best Regards!

回答1:


See: note down below Would it help to return Enumerable?

 return View(Q.AsEnumerable()); 

EDIT: Yeah, I know my first shot was wrong!... did not noticed that your view wants a

@model IEnumerable<NKI3.ViewModels.IndexCreateViewModel>

I agree with the others about converting to the required return value to the matching type... or adjust the expecting type in the view... maybe you can take advantage of mapping framworks like automapper (http://automapper.org/) to help you to solve to mapping code between domain objects and viewmodels.

it's up to you - No need for further down voting...



回答2:

Yes - the action should return a model of the same type declared on the first line of your view file.

Change your first line in the view file to:

@model IndexCreateViewModel

and change your action code (Index()) to a code that will return an object of type IndexCreateViewModel.



回答3:

The type returned by

db.Question.Include(a => a.SubjectType).Include(a => a.CoreValue);

looks to be IQueryable<NKI3.Models.Question>

You need to convert that structure (your data model) to an IEnumerable<IndexCreateViewModel>

If there isn't already a helper in your app to transform between them then you'll need to write one.

IList<IndexCreateViewModel> viewmodel = new List<IndexCreateViewModel>();

foreach(NKI3.Models.Question item in Q)
{
   IndexCreateViewModel viewItem = new IndexCreateViewModel();
   // I don't know the properties of Question.
   viewItem.QuestionText = item.????;
   viewItem.CName = item.???;
   viewItem.SName =-item.???;
   viewModel.Add(viewItem);
}
return View(viewModel);