Html没有正确呈现,并且没有使用Ajax.ActionLinks设置Viewbag.Title [英] Html not correct rendered and no Viewbag.Title is set using Ajax.ActionLinks

查看:82
本文介绍了Html没有正确呈现,并且没有使用Ajax.ActionLinks设置Viewbag.Title的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

_ViewStart.cshtml

@{
    Layout = Request.IsAjaxRequest() ? null : "~/Views/Shared/_Layout.cshtml";     
}

_Layout.cshtml

...
</head>
<body>
    <div id="NavigationPanel">
        @{ AjaxOptions options = new AjaxOptions
       {
           InsertionMode = InsertionMode.Replace,
           UpdateTargetId = "ContentPanel",
           OnBegin = "OnBeginAnimateContentPanel",
           OnComplete = "OnCompleteAnimateContentPanel",
       };}
        <div>
            <p>@Ajax.ActionLink("Users", "Index", "User", options)</p>
            <p>@Ajax.ActionLink("Groups", "Index", "Group", options)</p>
            <p>@Ajax.ActionLink("Permissions", "Index", "Permission", options)</p>
        </div>
    </div>
    <div id="ContentPanel">
        @RenderBody()
    </div>
</body>
</html>

Global.Asax.cs

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Main", action = "Index", id = UrlParameter.Optional }
    );
}

MainController.Index操作:

public ActionResult Index()
{
    return View();
}

主视图Index.cshtml

@{
    ViewBag.Title = "Test-Index";
}

this is the Index View of the MainController

用户/组/权限控制器的索引视图+操作类似于MainController的索引。标题设置为ViewBag和一些小测试文本。

The Index View + Action for the User / Group /Permission Controller are like the Index of the MainController. A Title is set to the ViewBag and some little test text.

这里有一个问题。

当我单击其中一个Ajax.ActionLInks时,虽然View在#ContentPanel中正确呈现,但每个视图的标题(用户/组/权限)永远不可见...但是当我检查html源代码时:

The title of each View (User/Group/Permission) is never visible although the View is rendered correctly in the #ContentPanel when I click one of those Ajax.ActionLInks...BUT when I check the html source code:

ContentPanel中的内容是索引视图中的内容???

The content in the ContentPanel is the one from the Index View ???

为什么是这样以及如何我可以为每个Ajax.ActionLink设置我的View标题吗?

<body>
  <div id="NavigationPanel">
    <p><a data-ajax="true" data-ajax-begin="OnBeginAnimateContentPanel" data-ajax-complete="OnCompleteAnimateContentPanel" data-ajax-mode="replace" data-ajax-update="#ContentPanel" href="/User">Users</a></p>
    <p><a data-ajax="true" data-ajax-begin="OnBeginAnimateContentPanel" data-ajax-complete="OnCompleteAnimateContentPanel" data-ajax-mode="replace" data-ajax-update="#ContentPanel" href="/Group">Groups</a></p>
    <p><a data-ajax="true" data-ajax-begin="OnBeginAnimateContentPanel" data-ajax-complete="OnCompleteAnimateContentPanel" data-ajax-mode="replace" data-ajax-update="#ContentPanel" href="/Permission">Permissions</a></p>
  </div>
  <div id="ContentPanel">
    this is the text within the Index View of the MainController
  </div>
</body>


推荐答案

当您查看来源时,您正在查看最初加载的来源;通过AJAX加载的内容不会出现。但是,如果使用DOM检查器,它将显示动态加载的内容。至少这是它在Chrome中的工作方式;我想其他浏览器是相同的。

When you view source, you're looking at the source that initially loaded; content loaded via AJAX will not appear. However, if you use the DOM inspector, it will show dynamically loaded content. At least this is the way it works in Chrome; I imagine other browsers are the same.

检查DOM检查器,看看代码是否符合预期(在Chrome和Firefox上右键单击Inspect element) )。

Check the DOM inspector, and see if the code is what you expect (right-click, "Inspect element" on Chrome and Firefox).

至于设置标题,用AJAX无法完成,至少没有一些Javascript帮助。在部分视图中,请尝试以下Javascript:

As for setting the title, that can't be done with AJAX, at least not without some Javascript help. In your partial views, try the following Javascript:

<script>
  document.title = "My New Title Here";
</script>

这篇关于Html没有正确呈现,并且没有使用Ajax.ActionLinks设置Viewbag.Title的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆