简体   繁体   中英

'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

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

I believe this is your problem from another stackoverflow answer:

As we can see by your comments, you are setting the ViewBag.Message in another controller. The ViewBag is only for data for the view that was set by the controller that served the view.

If you need to send it from another controller, you should use a parameter in your action method, then you can set the ViewBag.Message to the passed in parameter.

ViewBag RuntimeBinderException (MVC 4, ASPX)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM