The Model Item Passed into the Dictionary Is of Type .. But This Dictionary Requires a Model Item of Type

The model item passed into the dictionary is of type .. but this dictionary requires a model item of type

The error means that you're navigating to a view whose model is declared as typeof Foo (by using @model Foo), but you actually passed it a model which is typeof Bar (note the term dictionary is used because a model is passed to the view via a ViewDataDictionary).

The error can be caused by

Passing the wrong model from a controller method to a view (or partial view)

Common examples include using a query that creates an anonymous object (or collection of anonymous objects) and passing it to the view

var model = db.Foos.Select(x => new
{
ID = x.ID,
Name = x.Name
};
return View(model); // passes an anonymous object to a view declared with @model Foo

or passing a collection of objects to a view that expect a single object

var model = db.Foos.Where(x => x.ID == id);
return View(model); // passes IEnumerable<Foo> to a view declared with @model Foo

The error can be easily identified at compile time by explicitly declaring the model type in the controller to match the model in the view rather than using var.

Passing the wrong model from a view to a partial view

Given the following model

public class Foo
{
public Bar MyBar { get; set; }
}

and a main view declared with @model Foo and a partial view declared with @model Bar, then

Foo model = db.Foos.Where(x => x.ID == id).Include(x => x.Bar).FirstOrDefault();
return View(model);

will return the correct model to the main view. However the exception will be thrown if the view includes

@Html.Partial("_Bar") // or @{ Html.RenderPartial("_Bar"); }

By default, the model passed to the partial view is the model declared in the main view and you need to use

@Html.Partial("_Bar", Model.MyBar) // or @{ Html.RenderPartial("_Bar", Model.MyBar); }

to pass the instance of Bar to the partial view. Note also that if the value of MyBar is null (has not been initialized), then by default Foo will be passed to the partial, in which case, it needs to be

@Html.Partial("_Bar", new Bar())

Declaring a model in a layout

If a layout file includes a model declaration, then all views that use that layout must declare the same model, or a model that derives from that model.

If you want to include the html for a separate model in a Layout, then in the Layout, use @Html.Action(...) to call a [ChildActionOnly] method initializes that model and returns a partial view for it.

MVC - The model item passed into the dictionary is of type but this dictionary requires a model item of type 'System.Collections.Generic.List`1

Based on your error, it sounds like whatever model you specified in the cshtml file, is expecting a LIST of WebApplication1.Models.Location types, but you're only passing a single WebApplication1.Models.Location into it, as it clear by

return View("Location_List", l);

Either change the model that the cshtml file is expecting, to be a single location, or pass in a List of locations to the model. In fact, I think just changing that above line to this

return View("Location_List", loc);

will fix your issue. Cheers!

The model item passed into the dictionary is of type *** but this dictionary requires a model item of type ***

You forgot to use .First() or .FirstOrDefault().

This is necessary because the LINQ query itself returns a database object System.Data.Entity.Infrastructure.DbQuery hence why we need to save to memory and store to a variable with .First(),.FirstOrDefault(),.ToList(), etc.

public ActionResult Edit(int id)
{
var CompanyToEdit = (from m in _db.IPR_CompanyGen_200200501 where
(m.CompanyID.Equals(id.ToString())) select m);

// add .FirstOrDefault()
return View(CompanyToEdit.FirstOrDefault());
}

How to fix The model item passed into the dictionary is of type error?

You're trying to pass a collection to a view that's designed for a single object.

change your view declaration to

Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication3.Models.Customer>>

The model item passed into the dictionary is of type 'System.Collections.Generic.List but this dictionary requires a model item of type 'XXX.XXX.XXX'

if your View is supposed to be for UpdatePatient, then you want to create a model of type RegisterViewModel and add Genders to it, and then pass that model to the View, like this:


public ActionResult UpdatePatient()
{
List<SelectListItem> GenderList = GetGenders();
return View(new RegisterViewModel
{
FirstName = "",
MiddleName = "middle",
LastName = "last",
Genres = GenderList
});
}

The model item passed into the dictionary is of type 'List`, but this dictionary requires a model item of type 'PagedList.IPagedList`1

The Error Message is self explanatory. Your action method should return IPagedList<ModelNameSpace.Models.RMAHistory> and you are returning List<RMAHistory>.

So, your code would be:

 public ActionResult RMAList(string searchingrma, int? pageNumber) 
{

var query = db.RMAStatus.Join(db.RMA_History, u => u.ID, y => y.StatusID, (u, y) => new { u, y }).
Where(x => x.y.Ordrenummer.Contains(searchingrma) && x.y.Fakturnummer.Contains(searchingrma) || searchingrma == null).
Select(t => new RMAHistory
{

OrdreDato = t.y.OrdreDato,
AntalRMA = t.y.AntalRMA


}).OrderBy(t => t.OrdreDato).ToPagedList(pageNumber ?? 1, 5);


return View(query);
}

The model item passed into the dictionary is of type , but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable

Your query is passing List<MembershipType> to the view, but the view expects IEnumerable<ListMembershipTyp‌​eViewModel>

Change you GET method to

public ActionResult ListClubMembershipType(int clubId)
{

var types = from s in db.MembershipTypes
where (s.ClubId == clubId)
orderby s.Type
select s;
var model = types.Select(t => new ListMembershipTyp‌​eViewModel
{
Type = t.Type,
ClubId = clubId
});
return View(model);
}

Side note: Your method should not include the parameter ListMembershipTypeViewModel model



Related Topics



Leave a reply



Submit