Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to ViewBag.Title in ASP.NET MVC 3

Tags:

By default the new project template for ASP.NET MVC 3 adds the following to the default layout (masterpage in razor):

<title>@ViewBag.Title</title> 

The view must then contain the following to assign the title of the page, for instance:

@{      ViewBag.Title = "Log On"; } 

Perhaps it is just my own preference but I find that using the ViewBag to hold the title a little bit wrong (I'm thinking too much magic-string flavor). So my question is: Is this the recommended best practice for people using ASP.NET MVC 3 and razor (using a dynamic property bag) or are you opting for something more strongly typed (perhaps involving a custom baseclass?)

like image 675
soren.enemaerke Avatar asked Aug 08 '11 08:08

soren.enemaerke


People also ask

What is the use of ViewBag title?

From the ASP.NET site: ViewBag is a dynamic object, which means you can put whatever you want in to it; the ViewBag object has no defined properties until you put something inside it. Which sets the browser title.

Is it good to use ViewBag?

Yes, the ViewBag is bad. Strong typing is a best practice for many reasons (which you can research for yourself). I would use the ViewBag very sparingly.

How do I change the ViewBag value in Cshtml?

The ViewBag object value will be set inside Controller and then the value of the ViewBag object will be accessed in the cshtml file (View) using Razor syntax in ASP.Net MVC Razor.

What does return View () in MVC do?

View discovery The default behavior of the View method ( return View(); ) is to return a view with the same name as the action method from which it's called. For example, the About ActionResult method name of the controller is used to search for a view file named About. cshtml .


2 Answers

I don't think there is any thing bad with default title handling feature that ships with asp.net MVC 3, its okay to do.

I personally do this(below written) to handle title, I am not endorsing the below code or saying that its better than default functionality, its just a preference.

Master

<title>     @RenderSection("Title"); </title> 

View

@section Title {     write title } 

One thing i could suggest to improve default functionality

@{     string pageTitle = @ViewBag.Title ?? "Title Not Set"; } <title>@pageTitle</title> 

So whenever you forget to add it in viewbag, the page will display title= Title Not Set

Creating a base class then making all your controllers inherit from that base-class can also be done. But I think its taking so much of pain for title.

like image 88
Praveen Prasad Avatar answered Sep 28 '22 06:09

Praveen Prasad


ViewBag for title is perfectly fine (I'd even say it is the purpose of having ViewBag) - dynamic is not the absolute evil. "Title" is well known, unlikely to change and even predefined in view templates. I personally use following title:

<title>@(ViewBag.Title == null ? string.Empty : ViewBag.Title + " | ")Site Name</title> 

If you are worried about mistyping ViewBag.Title you can make it strong type by creating custom WebViewPage but you will still have to use ViewBag or maybe HttpContext.Items inside that strongly typed property because there are multiple instances of WebViewPage created during rendering IIRC.

I'd recommend to stick with ViewBag, creating own WebViewPage because of this seems like overkill - even creating single property on it if you already have custom WebViewPage is in my opinion just worthless complication - and that comes from person that is often overengineering things.

like image 31
Lukáš Novotný Avatar answered Sep 28 '22 05:09

Lukáš Novotný