MVC gives a strange error

The error message I get is:

The model element passed to the dictionary is of type "System.Data.Linq.DataQuery`1 [MvcApplication1.ContentPageNav]", but for this dictionary, a model element of type "MvcApplication1.ContentPageNav" is required.

public ActionResult Edit()
{
   DataClasses1DataContext dc = new DataClasses1DataContext();
   var model = from m in dc.ContentPageNavs
      select m;
   return View(model);
}

Any ideas on why I am getting this error? Any help would be appreciated.

+3
source share
5 answers

Try it (your code does not work due to the fact that you are expecting a ContentPageNav element, but you are sending a ContentPageNav list):

public ActionResult Edit()
{
   using(DataClasses1DataContext dc = new DataClasses1DataContext())
   {
     // here you can select some specific item from the ContentPageNavs list
     // Following query take first item from the list
     var model = dc.ContentPageNavs.FirstOrDefault();
     return View(model);
   }
}
+2
source

You select a list ContentPageNavin your variable model.

The view is expected ContentPageNav, not a list of them.

Try the following:

var model = (from m in dc.ContentPageNavs
  select m).FirstOrDefault();
+6

, ContentPageNav, LINQ.

return View(model.FirstOrDefault());

var model = dc.ContentPageNavs.FirstOrDefault();
+4

Because the error indicates that the types do not match. The view expects a single element, but you are passing a collection of elements. Try passing this as a model:(from m in dc.ContentPageNavs select m).FirstOrDefault();

+2
source

Look at your opinion, it is strictly printed. He should say something like

Inherits="System.Web.Mvc<ContentPageNav>"

if you need a list you may want to use

Inherits="System.Web.Mvc<IList<ContentPageNav>>"

or some list ... your LINQ might be wrong if it is not intended.

+2
source

Source: https://habr.com/ru/post/1794497/


All Articles