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>
精彩评论