gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 18:18:57 26 4
gpt4 key购买 nike

尽管我进行了所有搜索,但我对一些基本的 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”是一个“类型”,在给定的上下文中无效

最佳答案

您必须提供 MyProject_MVC.Models.UserRepository 的实例到局部 View 。 _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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com