Strongly typed Viewbag items

16,276

Solution 1

Use a ViewModel, which contains properties for all the different objects that you view needs

e.g.

public class MovieTransactionViewModel 
{
   public List<Transaction> Transactions { get; set; }
   public List<Movie> Movies { get; set; }
}

Then, have your view use this as it's model, and you will get intellisense in your view.

That way you are not changing your models, so EntityFramework will not be affected.

Solution 2

You could store it in a variable.

var movie = (Movie)ViewBag.Movie;

Then typing @movie. will produce intellisense for Name, Length.

Solution 3

Stay away from VieWbag: http://completedevelopment.blogspot.com/2011/12/stop-using-viewbag-in-most-places.html

Simply create a new VieWModel that contains other models within it.

Solution 4

No. That is because ViewBag is not strongly typed. It is implemented using dynamics and ExpandoObject. If you need a strongly typed model, why don't you use one?

Share:
16,276
John Mitchell
Author by

John Mitchell

One of the problems with current developers is a basic lack of programming knowledge. Sure people can pick up a book and learn C# or Java but that doesn't say much about if they can program. People learn way to much "parrot fashion" in schools and universities about how to solve a pre-set problem in the same way as everyone else, but no one seems to know how to program with core concepts any more. I'm different on that respect, I consider myself language agnostic, although I can program in many many languages I like having the ability and knowledge to pick up a language syntax manual and understanding it in a day or two, it means I can always pick the right language for the job.

Updated on June 17, 2022

Comments

  • John Mitchell
    John Mitchell almost 2 years

    If I am using a ViewBag which contains a strongly typed object, is there any way on MVC Razor to define (or cast this) so that all the elements of that appear in the IntelliSense?

    For example lets say I have

    ViewBag.Movies.Name
    ViewBag.Movies.Length
    

    I know Movies is of object type Movie, which has the members Name and length

    class Movie { 
        public string Name {get; set;}
        public string Length {get; set;}
    }
    

    Can I somehow cast this, like I do for model

    @model Transactions.UserTransactionDetails
    

    So that the members of Movie become available in Razor?

  • John Mitchell
    John Mitchell almost 12 years
    I'm already using a model to pass back and forth transaction information for a user form, so didn't think I could have 2 models :(
  • sam
    sam almost 12 years
    Can't you just add the data you need to your model? Are you using view models?
  • Wonko the Sane
    Wonko the Sane almost 12 years
    Its a good idea to use ViewModels (in general), but it is "required" if you are going to need to use entities from multiple models.
  • John Mitchell
    John Mitchell almost 12 years
    The Movie is actually held in a Entity Framework object. The other is held in a view model (user transaction). I'm doing validation using the user transaction model with validation flags set. When I was combining them both I couldn't get the validation to fire correct. Hence the putting one into viewbag :| Sorry still learning here :)
  • tocqueville
    tocqueville about 6 years
    Sometimes it's useful. For example, when you want to set the title and meda-description properties in the <head> section of _Layout.cshtml