开发者

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

开发者 https://www.devze.com 2023-02-07 00:35 出处:网络
I have a master page called Default.master. I want to set the title using ViewBag.Title. On my controller I have:

I have a master page called Default.master. I want to set the title using ViewBag.Title. On my controller I have:

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

My Home view uses Default.master as the master view page. On the master view page I am using:

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

But I get this error:

The call is ambiguous between t开发者_开发问答he following methods or properties: 'System.IO.TextWriter.Write(string, params object[])' and 'System.IO.TextWriter.Write(char[])'

How can I use it properly?


Try:

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


From this blog post I saw a syntax that I've never seen before:

<%: %>

This prints an expression, so using:

<%: ViewBag.Title %>

I got the correct value.


your ViewBag.Title's value is null. maybe there are some exceptions happened. only in this case the ambiguous exception will thrown

0

精彩评论

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

关注公众号