Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Who should know about the page title in Asp.Net MVC?

Should the Model, View or the Controller be the one that knows the page title?

Option 1

Use the model to store the page title (this makes the model more of a view-model):

public class SomeModel
{
    string Title { get; set; }
}

Then the view can simply set the title accordingly

<title>@Model.Title</title>

Option 2

The view knows its own title:

@{
     ViewBag.Title = "Some Title";
}

and somewhere in view or the default layout its being used:

<title>@ViewBag.Title</title>

Option 3

Set the title from the controller

ViewBag.Title = "Some Title"

And then use it from the view

<title>@ViewBag.Title</title>

Which is the best approach (assuming i have a common layout for the different pages) ?

like image 219
rony l Avatar asked Mar 08 '14 14:03

rony l


People also ask

What is the use of layout page in MVC?

Layouts are used in MVC to provide a consistent look and feel on all the pages of our application. It is the same as defining the Master Pages but MVC provides some more functionalities.

Is the default page in ASP.NET MVC?

In asp.net MVC the "homepage" (ie the route that displays when hitting www.foo.com) is set to Home/Index .


3 Answers

It depends. If all your titles are somewhat different and static I see no problem in each view "knowing" it's title.

However, if you need information from the model to make up the title then you have to... err... pass it from the model what doesn't really mean that you need a "Title" property but instead you can use something from the Model itself such as ProductName or anything else.

like image 118
tucaz Avatar answered Oct 29 '22 15:10

tucaz


It depends on your design. For example, if you had a generic view for displaying search results, you might want the title to relate to what the user searched for. In this case, it would likely end up in the model.

On the other hand, if you have a very specific view (e. g. a user profile page), then it makes more sense to let the view control it's own title. In such a case, you might have multiple views for a single model type (e. g. a homepage view and a compact profile view for a user), so the view will be driving the UI specifics and the model will be driving only the data behind the UI.

Both approaches can be useful (and you can find both within a single application).

Another factor that may drive this decision is how your engineering process works. For example, if views are primarily edited by front-end engineers who are focused on the look and feel, then it might be preferable to limit views to things like rendering HTML. On the other hand, if views tend to be worked on by UX designers who are focused more holistically on things like wording and the application domain, then it might make more sense to delegate such concerns to the view.

like image 22
ChaseMedallion Avatar answered Oct 29 '22 15:10

ChaseMedallion


I would always start by defining this in the view, and move it out only when you have a reason to do so (multiple views with the same title, dynamically created title, etc.).

In the case you would define it elsewhere, it really depends on why as to where you would define it. Do what makes sense. For instance, you might not want to define it in a view model if it were to be used by multiple views.

I don't think there's a right or wrong answer to this question, more depends on what you need.

like image 21
aw04 Avatar answered Oct 29 '22 14:10

aw04