开发者

Set title from controller/codebehind

开发者 https://www.devze.com 2023-02-21 09:46 出处:网络
Is it possible to set开发者_JAVA百科 the page title from the controller, and if so how is it done?

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

精彩评论

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