Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Late binding issue with MVC3 ViewBag in 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.

like image 311
CrispinH Avatar asked Feb 18 '11 14:02

CrispinH


People also ask

Why ViewBag is dynamic?

ViewBag is a dynamic type that allow you to dynamically set or get values and allow you to add any number of additional fields without a strongly-typed class They allow you to pass data from controller to view.

What is ViewBag in cshtml?

ASP.NET MVC - ViewBag. The ViewBag in ASP.NET MVC is used to transfer temporary data (which is not included in the model) from the controller to the view. Internally, it is a dynamic type property of the ControllerBase class which is the base class of the Controller class.

How to store data in ViewBag in MVC?

In Controller's Action Method in order to assign the Model data to ViewBag object we assign as shown below. In the below syntax, I am using employee object as well as a list of employee objects to store the data in ViewBag object and return ViewBag to the View or . cshtml and bind it to the HTML table.


1 Answers

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.

like image 100
Ed DeGagne Avatar answered Sep 23 '22 15:09

Ed DeGagne