传递到字典中的模型项的类型为..,但此字典需要类型为的模型项 [英] The model item passed into the dictionary is of type .. but this dictionary requires a model item of type

查看:159
本文介绍了传递到字典中的模型项的类型为..,但此字典需要类型为的模型项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已添加此问题和社区Wiki答案,以帮助解决许多未解决的问题,如

This question and community wiki answer has been added to assist in closing out numerous unanswered questions as discussed in this meta post.

我有一些代码,当它执行时,它抛出一个异常,说:

I have some code and when it executes, it throws an exception saying:

传递到字典中的模型项是Bar类型,但是此字典需要Foo类型的模型项

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

这是什么意思,我该如何解决?

What does this mean, and how do I fix it?

推荐答案

该错误表示您正在导航到其模型声明为typeof Foo的视图(通过使用@model Foo),但实际上已将其传递给它类型为Bar的模型(请注意,使用术语 dictionary 是因为模型是通过ViewDataDictionary传递给视图的.)

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).

该错误可能是由

将错误的模型从控制器方法传递到视图(或局部视图)

常见示例包括使用创建匿名对象(或匿名对象集合)的查询并将其传递给视图

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

通过在控制器中显式声明模型类型以匹配视图中的模型,而不是使用var,可以在编译时轻松识别错误.

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.

将错误的模型从视图传递到局部视图

给出以下模型

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

以及用@model Foo声明的主视图和用@model Bar声明的局部视图,然后

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); }

Bar的实例传递到局部视图.还要注意,如果MyBar的值是null(尚未初始化),则默认情况下Foo将传递给局部变量,在这种情况下,它必须是

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())

在布局中声明模型

如果布局文件包含模型声明,则所有使用该布局的视图都必须声明相同的模型或从该模型派生的模型.

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.

如果要在布局中包含单独模型的html,请在布局中使用@Html.Action(...)调用[ChildActionOnly]方法初始化该模型并返回其局部视图.

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.

这篇关于传递到字典中的模型项的类型为..,但此字典需要类型为的模型项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆