在_Layout.cshtml
视图中,我有
@using SiteNET.Utilities
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>"SomeTitle"</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
<!-- Favicons -->
<link rel="apple-touch-icon-precomposed"
sizes="256x256"
href="@Url.Content("~/Content/Images/SiteRetro256.png")">
<link rel="shortcut icon"
href="@Url.Content("~/Content/Icons/SiteRetro.ico")">
</head>
<body>
<div class="container">
<div class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<button type="button"
class="navbar-toggle"
data-toggle="collapse"
data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
</button>
@Html.ActionLink(SiteNET.Utilities.Constants.Site,
"Index", "Home", null, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="@Url.MakeActive("Home")">
@Html.ActionLink("Home", "Index", "Home")
</li>
<li class="@Url.MakeActive("Index", "Contacts")">
@Html.ActionLink("Contacts", "Index", "Contacts")
</li>
</ul>
@Html.Action("_LoginPartial", "Account") <- THIS LINE
</div>
</div> <!--container-fluid-->
</div> <!--navbar-->
@RenderBody()
<hr />
...
</div>
</body>
</html>
我的目标是将视图模型加载到_LoginPartial
视图中。所以我已将以下内容添加到AccountController
类
[ChildActionOnly]
public ActionResult _LoginPartial()
{
ApplicationUser user = null;
ManageUserViewModel model = null;
string userID = User.Identity.GetUserId() ?? String.Empty;
if (!String.IsNullOrEmpty(userID))
{
user = UserManager.FindById(userID);
model = new ManageUserViewModel();
model.IsAdmin = user.IsAdmin;
}
return PartialView(model);
}
但是这不会从
调用此方法@Html.Action("_LoginPartial", "Account")`
我已阅读this answer并已交换了
@Html.Action("_LoginPartial", "Account")
到
@Html.Action("_LoginPartial", "Account", new { area = "" })
因为控制器不在“Shared”文件夹中。我也试过
@Html.Action("_LoginPartial", "Account", new { area = "Controllers" })`
但我刚收到浏览器错误:
页面未正确重定向
我在这里做错了什么?
感谢您的时间。
编辑。按照@ markpSmith的建议,我试图使用
@{ Html.RenderAction("_LoginPartial", "Account"); }
_但这会产生同样的错误。 _
答案 0 :(得分:1)
使用@Html.Partial("_LoginPartial")
您无需在Controller中指定操作,因为您可以在View中访问User.Identity,因此使用User.Identity而不是Model更新_LoginPartial。
答案 1 :(得分:1)
我无法看到你是否尝试过:
@Html.RenderPartial()
ActionMethod(见MSDN)
让你看起来像:
@Html.RenderPartial("_LoginPartial","Account")
(在AccountController中)
public ActionResult _LoginPartial()
{
ApplicationUser user = null;
ManageUserViewModel model = null;
string userID = User.Identity.GetUserId() ?? String.Empty;
if (!String.IsNullOrEmpty(userID))
{
user = UserManager.FindById(userID);
model = new ManageUserViewModel();
model.IsAdmin = user.IsAdmin;
}
return PartialView(model);
}
除此之外,我建议删除下划线并查看是否可以运行它(因为它是部分视图,我不 认为 无论如何它都可以导航。)