如何更新@ Ajax.ActionLink的Html文档标题 [英] How to update Html document title with @Ajax.ActionLink

查看:62
本文介绍了如何更新@ Ajax.ActionLink的Html文档标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当点击一个Ajax.ActionLink,我想页面标题根据ViewBag.Title自动更改。我能做到的,但我不能访问从ActionExecutedFilterrazor.cshtml文件分配ViewBag.Title。我可以访问ViewBag.Title只能在离ActionExecutedFilter操作方法进行分配。

When an Ajax.ActionLink is clicked, I'd like page title to change automatically according to ViewBag.Title. I could do, but I cannot access ViewBag.Title assigned at "razor.cshtml" file from ActionExecutedFilter. I can access ViewBag.Title only assigned at Action Method from ActionExecutedFilter.

该ActionExecutedFilter可能是这样:

The ActionExecutedFilter could be so:

string scriptText = "<script type='text/javascript'>document.title = '" 
                    + ViewBag.Title + "';</script>";
filterContext.HttpContext.Response.Write(scriptText);

如何实现这一点?

How to achieve this?

推荐答案

我觉得这可以解决您的问题。然而,它不是使用ViewBag,但标题的价值也将与您的局部视图文件,如果我的理解以及它是你的意图。

I think this could solve your problem. However it is not using the ViewBag, but the value of the title will be also tied to your partial view file and if I understand it well it is your intention.

您的局部视图

<span id="mySpan" style="display:none">Page header</span>
@Ajax.ActionLink("MyLink", "Random", 
                 new AjaxOptions() { HttpMethod = "GET", 
                                     UpdateTargetId = "random", 
                                     OnSuccess = "document.title = document.getElementById('mySpan').innerHTML" 
                  });

<div id="random">
    Random goes here.
</div>

您想从您的局部视图文档标题​​是隐藏跨度内设置的值(也可能是也有些头值&LT; H1 /&GT; 或副标题&LT; H2 /方式&gt; ...要看你的需求)

The value you want to set from your partial view to document title is hidden inside the span (it could be also value of some header <h1/> or subheader <h2/> ... depends on your needs).

这篇关于如何更新@ Ajax.ActionLink的Html文档标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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