开发者

Set title from controller/codebehind

Is it possible to set开发者_JAVA百科 the page title from the controller, and if so how is it done?

Thanks M


You could simply add the title to the ViewBag and then display it in your view:

in controller:

ViewBag.PageTitle = "Your Page Title";

In the view/layout

 <head>
    <title>@ViewBag.PageTitle</title>
</head>

This is based on Razor's syntax, but works equally fine with the class ASPX syntax as well.


What I suggest is that you create a public property on your class and reference the property from the view page.

In your controller:

public class MyController : Controller
{
    public string Title { get; set; }

    public ActionResult Index()
    {
        this.Title = "My Title";

        return View();
    }
}

View:

<head>      
    <title><%= this.Title %></title>  
</head> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜