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!

3
  • And what happens if you use viewdata, do you get any errors then?
    – Jamie
    Feb 19, 2014 at 9:04
  • Possible duplicate question: stackoverflow.com/questions/6979629/… Good luck.
    – kryptonkal
    Feb 24, 2014 at 1: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, 2014 at 22:18

6 Answers 6

15

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.

1
  • 3
    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, 2023 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()

3
  • 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, 2015 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, 2015 at 9: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 8, 2016 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.

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)

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.

-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.

4
  • Are you asking question or answering? Mar 5, 2014 at 12:32
  • Simple English if you can understand Mar 5, 2014 at 14:44
  • Then this can't be answer! If you are facing any issue then click on ask question button to post a new question or you can leave a comment over the particular thread to ask for resolution or more clarification. Mar 6, 2014 at 2: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, 2014 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.