This project is read-only.

Compile time support for returning ViewResult from controller action

Jun 23, 2013 at 12:18 PM
All the views that I write are strongly-typed. I don't have to explain why this is important, since strong-typing is the main goal T4MVC tries to achieve.

Currently however, I'm using the following code in my controller action to return a view model to my view:
return this.View(new HomeViewModel());
But when I change the typing of my view to something else than HomeViewModel I get a runtime exception.

Is there a way to get compile-time support for this? I like the compiler to complain when my viewmodel doesn't match the view.

Something like:
return this.Views.Index(new HomeViewModel()); 

// or
return MVC.Controllers.Dinners.Index(
    new DinnerModel());
Is there a way to return a ViewResult from a controller action method in a way that is type safe?
Jun 23, 2013 at 4:37 PM
T4MVC does not support this. One reason is that it doesn't parse the views at all, which would be necessary to determine the view's type.
Jun 23, 2013 at 5:12 PM
Is it not possible for a T4 template to parse views or is it just too hard?
Jun 24, 2013 at 4:51 AM
I'm sure it's possible to parse that out. It's just not something that has come up yet, so no one has done it. Today, the only strong typing T4MVC supports for views is for the view name. e.g.
return View(Views.ViewNames.Index);