【问题标题】:Not Loading Partial View in MVC Application未在 MVC 应用程序中加载部分视图
【发布时间】: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


【解决方案1】:

使用@Html.Partial("_LoginPartial")

您无需在 Controller 中指定操作,因为您可以在 View 中访问 User.Identity,因此使用 User.Identity 而不是 Model 更新 _LoginPartial。

【讨论】:

  • 但是在视图中我必须让用户喜欢user = UserManager.FindById(userID) 所以必须实例化UserManager 可以这样做吗?
  • 您可以使用 User.IsInRole("Admin") 代码检查登录用户是否为管理员,因此我认为您不需要来自 UserManager 的用户信息。有意义吗?
  • 有点,但我没有使用角色(这可能被认为不好!)。原因是应用程序非常简单,只有一个管理员用户。所以我在用户数据库中存储了一个名为IsAdmin 的字段。所有用户都创建IsAdmin=false,除了一个名为Administrator 的用户在首次创建数据库时设置了IsAdmin =true。不过,我正在接受您的建议并检查视图中的User.IsAdmin...
  • 我建议改为调用 Action ,因为这个原因您可以创建 BaseController 并覆盖 OnActionExecuting 并根据您的条件设置 ViewBag.IsAdmin true 或 false。并从 BaseController 继承所有控制器。
【解决方案2】:

我看不到您是否尝试过:

@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);
}

除此之外,我建议删除下划线并查看是否可以运行它(因为它是部分视图,我不认为它会无论如何都可以导航)

【讨论】:

  • @Killercam 无论如何,我都是用它来打开局部视图。我从来没有听说过/使用过 {areas =...} ,所以不完全确定这是必需的。
  • 一件事是RenderPartial返回void,所以你可以把它“代替”这个@Html.Action方法......
猜你喜欢
  • 1970-01-01
  • 2012-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-16
  • 1970-01-01
  • 2015-04-05
  • 1970-01-01
相关资源
最近更新 更多