Is it possible to set page title using ViewBag in ASPX mode?

I have a main page with a name Default.master. I want to set the title using ViewBag.Title. On my controller, I have:

public ActionResult Index()
{
    ViewBag.Title = "Home";
    return View();
}

In my view Home, Default.masterit is used as the main viewing page. On the browse main page, I use:

<title><%= ViewBag.Title %></title>

But I get this error:

The call is ambiguous between the following methods or properties: 'System.IO.TextWriter.Write(string, params object[])' and 'System.IO.TextWriter.Write(char[])'

How can I use it correctly?

+5
source share
3 answers

From this blog post, I saw syntax that I had never seen before:

<%: %>

This prints the expression, so use:

<%: ViewBag.Title %>

I got the correct value.

+7
source

Try:

<title><%= (string) ViewBag.Title %></title>
+7

ViewBag.Title null. , .

+1

All Articles