10

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.

Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70
CrispinH
  • 1,899
  • 4
  • 23
  • 38
  • I just created a new VB.Net project using the Internet Application template for MVC4 RC and it used the ViewBag property. It did not build until I replaced it with ViewData. – Scott Munro Jun 18 '12 at 06:18

2 Answers2

15

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.

Ed DeGagne
  • 3,250
  • 1
  • 30
  • 46
0

The mayor problem with ViewBag in VB (and the reason why the VB template uses ViewData) is that the VB binder does not work with things typed as dynamic in medium trust. Try setting your app to full trust.

Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70
marcind
  • 52,944
  • 13
  • 125
  • 111
  • I changed the web.config file to Full Trust but it's not making any difference to the compile time error. I haven't had trust issues before, so this would be a new avenue for me. – CrispinH Feb 18 '11 at 20:50