17

I have a really simple ViewBag.Title. Like this:

@{
    ViewBag.Title = "My Title";
    ViewBag.MiniTitle = "Sub - Title";
}

Which is being parsed on _Layout.cshtml, on the

<title>@ViewBag.Title</title>

However, I am getting this exception:

Thrown: "'System.Dynamic.DynamicObject' does not contain a definition for 'Title'"
(Microsoft.CSharp.RuntimeBinder.RuntimeBinderException) 
Exception Message = "'System.Dynamic.DynamicObject' does not contain a definition for 
'Title'", Exception Type = "Microsoft.CSharp.RuntimeBinder.RuntimeBinderException"

I been searching and I could not find much information about it. It is basically the same issue this guy was facing: http://forums.asp.net/t/1715878.aspx?MVC3+Razor+Viewbag+Title+RuntimeBinderException

My issue is also the same as last guy that posted. This does not cause any problems for me, projects works fine and so does my titles. However, I am not liking the fact that an exception is being thrown due to the fact they are expensive.

Does anyone know how can i fix this issue? Thanks!

tereško
  • 58,060
  • 25
  • 98
  • 150
JohnC1
  • 841
  • 1
  • 12
  • 27
  • And what happens if you use viewdata, do you get any errors then? – Jamie Feb 19 '14 at 09:04
  • Possible duplicate question: http://stackoverflow.com/questions/6979629/alternative-to-viewbag-title-in-asp-net-mvc-3?rq=1 Good luck. – kryptonkal Feb 24 '14 at 01:00
  • I have not tried Viewdata. I am following the default ViewBag.Title and yet it throws an exception. That can't be normal. Also, I don't think your link is duplication AT ALL, to this question. Were talking about different things. – JohnC1 Mar 13 '14 at 22:18

6 Answers6

12

Using the Hot Reload function in VisualStudio 2022 can cause this error to occur seemingly random, without any problems in the code.

A fix is to stop running/debugging, build and restart.

I'm using VS2022 version 17.1.6 currently.

MeanGreen
  • 3,098
  • 5
  • 37
  • 63
  • 2
    I'm sure VS 2022 wasn't the issue when the question was asked in 2013 but your hint helped me a lot :-D – Paul B. Jan 18 '23 at 11:27
3

Could be that you are using @ViewBag.Title before you declare it, for example if your layout file has

<title>@ViewBag.Title</title>

but you define the title LATER in a partial view or similar

@{
    ViewBag.Title = "My Title";
    ViewBag.MiniTitle = "Sub - Title";
}

try setting ViewBag.Title in the controller action, so that is available before you call View()

Kaido
  • 3,383
  • 24
  • 34
  • That could be the issue. From the View.chstml, @{ ViewBag is defined }, however, it is being called first on Layout.cshtml before RenderBody(). So that may be it but then how else are you supposed to do it? That's the standard way. – JohnC1 Mar 15 '15 at 20:37
  • I prefer the use of ViewModels instead as a more robust solution for enterprise applications. For small amounts of data it is okay to set the ViewBag in the action in the controller. Setting ViewBag in the views is generally a bad idea. – Kaido Mar 16 '15 at 09:20
  • I just ran into this issue too and the weird part about it is that it is included with the default asp.net template – CSharper Jan 08 '16 at 14:50
1

i tried this and it works for me:

@(ViewBag.GetType().GetProperty("Title") == null ? "" : ViewBag.Title)

it's an old question but maybe it helps someone.

regards.

Muzaffer Galata
  • 580
  • 1
  • 9
  • 22
1

Your DLL is corrupt. I fixed it with me by copying Microsoft.CSharp.dll from my colleague. Location is C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.7 (Last folder for version you adjust according to yours)

Nico
  • 121
  • 1
  • 2
1

I was getting a similar issue. The only way I could fix it was by changing ViewBag.Title, for instance, to ViewData["Title"]. Technically, ViewBag and ViewData are the same thing and ViewData is faster to use but you have to type cast... But if it was a string anyway it appears to work fine with no cast since it'll call ToString automatically.

Daniel Lorenz
  • 4,178
  • 1
  • 32
  • 39
-1

I tried to regenerate said issue without luck.

You have set ViewBag.Title in Master page and this error seems occuring from another View where you are not defining ViewBage.Title property.

Since, ViewBag.Title is local variable of Master page, other View will not get it and throws said Error.

Now, try using ViewBag.MiniTitle in tag. Are you still getting same error?

Please Share.

Dipal Mehta
  • 440
  • 6
  • 20
  • Are you asking question or answering? – Paresh Mayani Mar 05 '14 at 12:32
  • Simple English if you can understand – Dipal Mehta Mar 05 '14 at 14:44
  • Then this can't be answer! If you are facing any issue then click on [ask question](http://stackoverflow.com/questions/ask) button to post a new question or you can leave a comment over the particular thread to ask for resolution or more clarification. – Paresh Mayani Mar 06 '14 at 02:45
  • Please look at the link I provided. I believe it shows you exactly how to receive this error. It's not really an error but you know what I mean. Any Viewbag throws that exception. – JohnC1 Mar 13 '14 at 22:16