[英]Not Loading Partial View in MVC Application
In my _Layout.cshtml
view I have 在我的
_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>
My aim is be able to load a view model into my _LoginPartial
view. 我的目标是能够将视图模型加载到
_LoginPartial
视图中。 So I have added the following to my AccountController
class 所以我在我的
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);
}
But this does not call this method from 但这不会从
@Html.Action("_LoginPartial", "Account")`
I have read this answer and have swapped the 我已阅读此答案并交换了
@Html.Action("_LoginPartial", "Account")
to 至
@Html.Action("_LoginPartial", "Account", new { area = "" })
as the controller is not in the "Shared" folder. 因为控制器不在“共享”文件夹中。 I have also tried
我也尝试过
@Html.Action("_LoginPartial", "Account", new { area = "Controllers" })`
but I am just getting a browser error: 但我只是遇到浏览器错误:
The page isn't redirecting properly
页面未正确重定向
What am I doing wrong here? 我在这里做错了什么?
Thanks for your time. 谢谢你的时间。
Edit. 编辑。 following @markpSmith's suggestion, I have attempted to use
按照@markpSmith的建议,我尝试使用
@{ Html.RenderAction("_LoginPartial", "Account"); }
_but this give the same error. _但这给出了相同的错误。 _
_
Use @Html.Partial("_LoginPartial")
使用
@Html.Partial("_LoginPartial")
You don't need to specify action in Controller as you can access User.Identity in View so update _LoginPartial with User.Identity instead of Model. 您无需在Controller中指定操作,因为您可以在View中访问User.Identity,因此用User.Identity而不是Model更新_LoginPartial。
I can't see if you have attempted the: 我看不到您是否尝试过:
@Html.RenderPartial()
ActionMethod (see MSDN ) ActionMethod(请参阅MSDN )
making yours look similar to: 使您的外观类似于:
@Html.RenderPartial("_LoginPartial","Account")
(within AccountController) (在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);
}
Other than that, I would suggest removing the underscore and see if you can run it instead (since it is a partial view, I don't think it will be navigateable anyway) 除此之外,我建议删除下划线并查看是否可以运行它(因为它是局部视图,所以我认为无论如何它都无法导航)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.