.NET MVC3获得一个参考页面从控制器 [英] .Net MVC3 Getting a Reference to the Page from a Controller

查看:106
本文介绍了.NET MVC3获得一个参考页面从控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的似乎pretty简单。我想在我的@Page指令指定在视图页面标题,就像这样:

What I'd like to do seems pretty simple. I'd like to specify the page title in my @Page directive on a view, like so:

<%@ Page Title="About This Site" ... %>

然后,我想获取页面标题到ViewData的,这样就可以在母版页中使用,如:

Then, I'd like to get that page title into the ViewData, so that it could be used in the master page, like:

<head>
    <title>The Awesome Site | <%=ViewData["Title"]%></title>

和也是在视图本身,如:

And also in the View itself like:

<h1><%=ViewData["Title"]%></h1>

我已经继承了所有的页面控制器方法定制控制器。所以,我希望有什么东西我可以在控制器的Page.Title财产转移到ViewData的(也许我将使用ViewBag)做的。我似乎无法找到任何途径从控制器中引用的页面。

I am already inheriting a custom controller for all of the page controller methods. So I'm hoping that there's something I can do in the controller to transfer the Page.Title property into the ViewData (or maybe I'll use the ViewBag). I just can't seem to find any route to reference the Page from within the Controller.

这可能吗?是否有不同的方法,我可能要考虑。

Is this possible? Is there a different approach I might want to consider.

推荐答案

请参阅本通用的错误后一个干净的方式来做到这一点。

See "generic errors" post on this for a clean way to do this.

数据传递给母版页的ASP.NET MVC

你不应该参考从页从控制器东西。这两个在设计上完全分开的。您需要打包的数据,然后将其发送到视图。
另一种方法(更喜欢你所请求的)是使用
@ {

you should not be referencing anything from a "page" from a controller. The two are completely separate by design. You need to package your data and then send it to the view. Another way (more like what you are requesting) is that you use @{

ViewBag.Title =我的观点题;
}
然后母版页使用,无论你想它该值。

ViewBag.Title ="my view title"; } Then the master page uses that value wherever you want it.

这篇关于.NET MVC3获得一个参考页面从控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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