'System.Dynamic.DynamicObject'不包含'PropertyName'的定义

时间:2022-12-07 16:53:22

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.

我正在尝试运行.NET MVC站点以使用ViewBag将一些数据传递给我的View。这是我设置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

当我为ViewBag赋值(例如ViewBag.BanReason = null;)时,我得到了带有消息的RuntimeBinderException

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

'System.Dynamic.DynamicObject'不包含'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.

我正在使用带有.NET 4.5的Visual Studio 2012和带有.NET 4.0的ApplicationPool的本地IIS服务器。在具有相同配置的另一台计算机上,所有工作都很好,并且不会抛出任何异常。

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.

请不要建议使用其他技术,如ViewData [“”]和创建单独的模型,我不想更改在其他机器上运行良好的代码。

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

我认为这是错过配置问题。任何猜测将不胜感激。谢谢

2 个解决方案

#1


16  

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

您的代码很好,ViewBag具有动态属性,并且在运行系统中正在分配所有属性。

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

这个异常是一个常见的CLR异常。尝试执行以下步骤,我希望它能工作。打开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

点击这里查看详细图片

#2


3  

I believe this is your problem from another * 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.

正如我们在您的评论中看到的那样,您正在另一个控制器中设置ViewBag.Message。 ViewBag仅用于由提供视图的控制器设置的视图的数据。

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.Message设置为传入的参数。

ViewBag RuntimeBinderException (MVC 4, ASPX)

ViewBag RuntimeBinderException(MVC 4,ASPX)

#1


16  

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

您的代码很好,ViewBag具有动态属性,并且在运行系统中正在分配所有属性。

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

这个异常是一个常见的CLR异常。尝试执行以下步骤,我希望它能工作。打开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

点击这里查看详细图片

#2


3  

I believe this is your problem from another * 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.

正如我们在您的评论中看到的那样,您正在另一个控制器中设置ViewBag.Message。 ViewBag仅用于由提供视图的控制器设置的视图的数据。

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.Message设置为传入的参数。

ViewBag RuntimeBinderException (MVC 4, ASPX)

ViewBag RuntimeBinderException(MVC 4,ASPX)