Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'System.Dynamic.DynamicObject' does not contain a definition for 'PropertyName'

I've faced with the next problem:

I'm trying run .NET MVC site to use ViewBag for passing some data to my View. Here is the method where I setting the VeiwBag.

[HttpGet] [AllowAnonymous] public ActionResult Start() {     ViewBag.BanReason = null;     int userId;      //Request with UserId, display ban reason if necessary     if (Request.Params["UserId"] != null && int.TryParse(Request.Params["UserId"], out userId))     {         ViewBag.BanReason = CheckIfUserBanned(userId);     }      ViewBag.Users = MvcApplication.Users;      return View(); } 

When I assign value to the ViewBag (for example ViewBag.BanReason = null;) I've got the RuntimeBinderException with the message

'System.Dynamic.DynamicObject' does not contain a definition for 'BanReason'.

I'm using Visual Studio 2012 with .NET 4.5 and local IIS server with ApplicationPool with .NET 4.0. On the other computer with the same configuration all works nicely and no exceptions is thrown.

Please don't advice to use other techniques like ViewData[""] and Creating a separate model, I don't want to change code that works nicely on the other machine.

I think this is miss-configuration issue. Any guesses will be appreciated. Thanks

like image 371
Groove Avatar asked Aug 22 '13 14:08

Groove


1 Answers

your code is fine, ViewBag is having a Dynamic Properties, and in Runtime all the properties are being assigned.

This Exception is a common CLR Exception.. Try doing the Following steps it will work I hope. Open Visual Studio

  • Debug --> Exceptions --> (Uncheck) Common Language Runtime Exceptions

if still problems occures

  • Tools --> Options --> Debugging(Expand) --> General --> (Check) Enable Just My Code

hope it helps, its working for me

Click Here for Detailed Images

like image 88
Gowtham.K.Reddy Avatar answered Sep 23 '22 14:09

Gowtham.K.Reddy