【发布时间】:2014-11-10 12:59:22
【问题描述】:
在我的_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 = "" })
因为控制器不在“共享”文件夹中。我也试过了
@Html.Action("_LoginPartial", "Account", new { area = "Controllers" })`
但我只是收到浏览器错误:
页面未正确重定向
我在这里做错了什么?
感谢您的宝贵时间。
编辑。按照@markpSmith 的建议,我尝试使用
@{ Html.RenderAction("_LoginPartial", "Account"); }
_但是这给出了同样的错误。 _
【问题讨论】:
-
你试过
Html.RenderAction吗? -
不,我现在就试试这个。谢谢。 Downvoter,这不超出了论坛的范围。这个问题提出得很好,并且包含所有必需的信息......
-
同意上述观点 - 如果没有反馈,反对票是毫无意义的,因此请用赞成票将其取消。
-
干杯。我只是在研究如何使用
RenderAction。再次感谢... -
这会在浏览器中出现同样的错误,无一例外...
标签: c# asp.net asp.net-mvc-5