Search code examples
c#asp.net-mvc-3strongly-typed-view

What is the best way to pass strongly typed variables to the view


Is there any way that I can pass multiple variables to a view so I can work with the type members and methods?

I want to be more safe with my coding? ViewData will not pass strongly typed.

ASP.net 4 MVC 3 Thanks


Solution

  • Usually the way to do this is to create a view-model that has the various values you need, i.e.

    public class SomeSpecificViewModel {
        public int SomeValue {get;set;}
        public string AnotherValue {get;set;}
        public FancyType MoreComplexValue {get;set;}
    }
    

    Now pass a SomeSpecificViewModel to the view, and tell your view to expect a SomeSpecificViewModel. Then just access the members.