在模板 MVC 3 项目中调用 Html.Partial(“LogOnPartial”) 时出错

发布于 2024-10-15 11:39:37 字数 718 浏览 4 评论 0原文

一切都开箱即用,但在添加使用模型“MySite.Models.Cars”返回强类型视图的新控制器后,在 SiteLayout.cshtml 中调用 Html.Partial("LogOnPartial") 时出现错误。我这样称呼新视图:

return View(db.Cars.FirstOrDefault());

这是错误:

模型项传入 字典的类型是 'MySite.Models.Cars',但是这个 字典需要一个模型项 输入“MySite.Models.LogOnModel”。

非常令人沮丧。当我使用新视图而不提供模型时,它会再次工作。

return View();

我在 MVC 2/ASPX 中设置了完全相同的条件,并且效果很好。我不确定这是 MVC 2/MVC 3 还是 ASPX/Razor 问题。事实上,这可能是一个深夜问题...

非常感谢您的回复。

编辑:

LogOnPartial.cshtml 的第一行(我删除了下划线)

@model MySite.Models.LogOnModel

Index.cshtml 的第一行(新视图):

@model MySite.Models.Cars

Everything works fine out of the box, but after adding a new controller that returns a strongly typed view using the model 'MySite.Models.Cars', i get the an error when calling Html.Partial("LogOnPartial") in SiteLayout.cshtml. I call the new view like this:

return View(db.Cars.FirstOrDefault());

This is the error:

The model item passed into the
dictionary is of type
'MySite.Models.Cars', but this
dictionary requires a model item of
type 'MySite.Models.LogOnModel'.

Very frustrating. When i use the new view without supplying a model it works again.

return View();

I setup the exact same conditions in MVC 2/ASPX and it worked just fine. I am not sure whether this is an MVC 2/MVC 3 or ASPX/Razor issue. In fact it might be a late night issue...

Very thankful for any reply.

EDIT:

First line from LogOnPartial.cshtml (i removed the underscore)

@model MySite.Models.LogOnModel

First line from Index.cshtml (new view):

@model MySite.Models.Cars

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

往日 2024-10-22 11:39:37

默认情况下,模板创建的 _LogOnModel.cshtml 部分不是强类型的,并且不需要任何模型。如果您对其进行了修改,使其需要一个模型,则在调用它时需要传递此模型:

@Html.Partial("_LogOnPartial", SomeLogonModelInstance)

By default the _LogOnModel.cshtml partial as created by the template is not strongly typed and doesn't require any model. If you modified it so that it requires a model you will need to pass this model when calling it:

@Html.Partial("_LogOnPartial", SomeLogonModelInstance)
夜未央樱花落 2024-10-22 11:39:37

从您的视图中发布代码。 (至少前几行)。您的视图需要某种类型的模型(LogOnModel),但您传递的是不同的类型(汽车)。

您应该在视图的第一行中看到引用的 LogOnModel。如果您看到错误,请继续修复它,如果没有,请在此处发回您的视图副本,我们将为您提供帮助。

Post the code from your View. (atleast the first few lines). Your view is expecting a Model of a certain type (LogOnModel) but you're passing in a different type (Cars).

You should see this LogOnModel referenced in the first line of your View. If you see the error, go ahead and fix it, if not post back here with a copy of your view and we'll help you out.

不可一世的女人 2024-10-22 11:39:37

看来这可能是一个深夜的问题。部分的类型是什么?从错误读取的内容来看,当部分正在寻找 LogOn 模型时,您似乎正在传递 Cars 模型类型。这可能是您创建局部时自动创建的内容。

It seems as though it might be a late night issue. What is the type of the partial? From what the error reads it seems as though you are passing in a Model type of Cars when the partial is looking for a Model of LogOn. This could have been something that was automatically created when you created the Partial.

将军与妓 2024-10-22 11:39:37

看来您正在 _Layout 页面中使用自定义成员身份和部分视图 (_LogOnPartial)。默认情况下,_Layout 页面没有控制器,因此如果没有必要,请勿在其上使用任何模型。将您的 LogOnModel 移出 _Layout 页面,并让 LogOff 和 LogOn ActionLikns 保留在 _LogOnPartial 上,就像 EF 默认成员身份方法一样。

It seems you're using Custom Membership and a partial view (_LogOnPartial) in _Layout page. By default _Layout page does not have a controller so don't use any model on it if is not necessary. Put your LogOnModel out of the _Layout page and let LogOff and LogOn ActionLikns remain on _LogOnPartial just like EF default membership approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文