asp.net-mvc-3 - 部分 View : "The model item passed into the dictionary is of type"

标签 asp.net-mvc-3

尽管我进行了很多搜索,但我对一些基本的 MVC 概念缺乏理解。

我在 Visual Studio 中创建了一个 MVC 项目,其中包含部分 View _LogOnPartial.shtml。我只想访问与用户相关的 View 中的信息,并将其放入用户下拉菜单中。当我尝试将其放在部分 View cshtml 页面的顶部时,出现上述错误:

@model MyProject_MVC.Models.UserRepository

当我尝试此操作时,我也收到错误:

@Html.Partial("_LogOnPartial", MyProject_MVC.Models.UserRepository)

“MyProject_MVC.Models.UserRepository”是一个“类型”,在给定上下文中无效

最佳答案

您必须向分部 View 提供 MyProject_MVC.Models.UserRepository 的实例。 _LogOnPartial 是该类型的强类型。它允许您在编译时访问其公共(public)成员并决定如何在 View 中显示它。

如果您想在该 View 中使用自己的类型,首先必须更改其强类型类型。

@model MyProject_MVC.Models.UserRepository

然后,您必须在操作方法中创建该类型的实例,并将其作为模型或模型的属性传递给 View 。

public ActionResult LogOn()
{
    return View(new UserRepository());
}

现在您可以在 View 中将此实例作为模型对象进行访问。

@Html.Partial("_LogOnPartial", Model);

关于asp.net-mvc-3 - 部分 View : "The model item passed into the dictionary is of type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11102239/

相关文章:

c# - 具有多个存储库的 ASP.NET MVC Controller ?

asp.net-mvc-3 - Controller Action 单元测试的模拟默认角色提供者

.net - ASP.NET:跨应用程序的表单例份验证:匹配密码加密设置

asp.net - 在 ActionLink 内添加 div

asp.net-mvc - 为什么我在 IE 中得到 'Mozilla'?

c# - 如何从 Controller 触发弹出确认

asp.net-mvc - 如何在全局级别添加 AuthorizeAttribute 并在某些操作中排除它?

json - 为什么更新操作没有发布任何数据?

asp.net-mvc-3 - C# lock 关键字,我想我用错了

asp.net-mvc-3 - 为什么 Visual Studio 找不到 System.Web.Mvc.resources.dll?