views:

137

answers:

3

I saw somewhere code like this:

return View(x=>x.List());

Instead of

return View("List");

What do I need to achieve this ?

I'm using Asp.net MVC 2 RC 2

EDIT I do not mean strong typed views

Next example

return this.RedirectToAction(c => c.Speaker());
+2  A: 

It's not the controller that's strong typed... it's the view.

To get a strongly typed view, you can either use the prompts from the VS MVC tools, and right click on an action and choose "Create a strongly-typed view", then select the proper business object to act as your model, or you can directly alter a page by changing it's Page directive's Inherits attribute to System.Web.Mvc.ViewPage, where SomeModel is the model that implements the "List" property and is the model that will be bound to the page.

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SomeModel>" %>

Also, I believe what you're thinking about is the usage on the View:

<%= Html.LabelFor(m -> m.SomeValue) %>

Again, I don't believe you're thinking about a strongly typed controller, I'm pretty sure what you saw was a strongly typed view.

If you go through the NerdDinner tutorial, you'll see this kind of thing time and time again.

David Morton
I know what's a strong typed view,but i mean something else. You can use it for example in RedirectToAction calls like:return this.RedirectToAction(c => c.Speaker());
+3  A: 

Strongly Typed RedirectToAction is provided by the MvcContrib project.

return RedirectToAction(c => c.Speaker());

return RedirectToAction<OtherController>(c => c.Speaker());
Lachlan Roche
It use the magic System.Linq.Expression. c.Speaker() is compiled into Expression that allows the library extract string "Speaker" from it.
Dennis Cheung
+1  A: 

I am not sure what you would expect as a return from a call to a View method taking a different controller action as a parameter. As you have pointed out RedirectToAction has this behaviour as well as some Html helper methods such as:

<%= Html.ActionLink<myController>(x => x.Index(), "My Action") %>
Andy Rose