How do you pass values from Controller to View with MVC3


The scenario: Given a blog application, with the following layoutimage

with two possible usages – a post page: imageand a homepage:image

Let’s define the view model:

  string Title
  string Body

  PostData Post; 

  PostData[] Posts

  Tuple<string, int>[] Archive
  Tuple<string, int>[] TagCloud
  string[] Similar

The views:

How data moves around:

Given that I want typed access to the view parameters in the view (for the sake of intellisense and refactorings), how would I model and pass the data around?

I’ve pseudo-code-grade written two options: the first is to use inheritence in the view model to achieve type-ness, on the expense of flexibility (composition is difficult with class hierarchy, and you need to be aware of and grab the viewModel instance in various places). The second is flexible (use the ViewData dictionary) but getting type-ness is cumbersome and partial (strings scattered around, casting needed etc.)

see if the gist widget does not load in-place I do have a solution that works for me With the many years that I’ve been writing complex web apps using various ASP.NET frameworks and almost always with c# based, static-typed view engines, I have a solution that works very nicely for me. But I want to be aware of the MVC3 canonical / textbox way So for all you MVC3 ninja’s out there – please describe your way of doing it.

I will describe my approach in an upcoming post and I’d appreciate any input on it

     Tweet Follow @kenegozi