In a simple Viewbag.Title, getting a RuntimeBinderException
Asked Answered
N

6

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!

Nibble answered 31/12, 2013 at 19:28 Comment(3)
And what happens if you use viewdata, do you get any errors then?Iodism
Possible duplicate question: #6980129 Good luck.Blear
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.Nibble
W
16

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.

Wofford answered 15/6, 2022 at 9:3 Comment(1)
I'm sure VS 2022 wasn't the issue when the question was asked in 2013 but your hint helped me a lot :-DCenis
H
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()

Hawkweed answered 13/3, 2015 at 13:36 Comment(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.Nibble
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.Hawkweed
I just ran into this issue too and the weird part about it is that it is included with the default asp.net templateWeinhardt
A
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.

Allotrope answered 21/5, 2016 at 8:9 Comment(0)
H
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)

Hudgens answered 25/9, 2018 at 13:17 Comment(0)
I
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.

Inconsolable answered 30/5, 2020 at 12:52 Comment(0)
E
-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.

Expectorate answered 5/3, 2014 at 12:1 Comment(4)
Are you asking question or answering?Jacksmelt
Simple English if you can understandExpectorate
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.Jacksmelt
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.Nibble

© 2022 - 2024 — McMap. All rights reserved.