VB.NET 中 MVC3 ViewBag 的后期绑定问题
我正在 VB.NET MVC3 项目中尝试 MVC 脚手架,并遇到了启用 Option Strict 的后期绑定问题(我希望启用它)。
这在 C# 中有效:
public ActionResult Create()
{
ViewBag.PossibleTeams = context.Teams;
return View();
}
但在 VB.NET 中几乎相同的代码:
Public Function Create() As ActionResult
ViewBag.PossibleTeams = context.Teams
Return View()
End Function
会导致编译器错误Option Strict On 不允许后期绑定。我查看了这里的文档: http://msdn.microsoft.com/en-us/library/system.web.mvc.controllerbase.viewbag(VS.98).aspx 但这不是很有帮助。
我注意到 C# 中的新空应用程序使用 HomeController
中的 ViewBag
,但 VB.NET 版本使用 ViewData
,所以这可能是一个VB.NET 的限制。
I'm trying out MVC Scaffolding in a VB.NET MVC3 project and running into an issue with late binding with Option Strict set on (and I want it on).
This works in C#:
public ActionResult Create()
{
ViewBag.PossibleTeams = context.Teams;
return View();
}
but the virtually the same code in VB.NET:
Public Function Create() As ActionResult
ViewBag.PossibleTeams = context.Teams
Return View()
End Function
causes the compiler error Option Strict On disallows late binding. I took a look at the documentation here: http://msdn.microsoft.com/en-us/library/system.web.mvc.controllerbase.viewbag(VS.98).aspx but it wasn't very helpful.
I notice that a new empty application in C# uses the ViewBag
in the HomeController
but the VB.NET version uses ViewData
, so maybe this is a VB.NET limitation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是信任问题。 Option Strict On 不允许后期绑定。在 VB.Net 中,请改用 ViewData 对象并保持 Option Strict On 设置。
This is not a Trust issue. Option Strict On disallows late binding. In VB.Net, use the ViewData object instead and maintain your Option Strict On setting.
VB 中
ViewBag
的主要问题(以及 VB 模板使用ViewData
的原因)是 VB 绑定器无法处理中等信任度中动态类型的内容。尝试将您的应用程序设置为完全信任。The mayor problem with
ViewBag
in VB (and the reason why the VB template usesViewData
) is that the VB binder does not work with things typed as dynamic in medium trust. Try setting your app to full trust.