user137348
user137348

Reputation: 10332

Asp.NET MVC strong typed controllers

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());

Upvotes: 2

Views: 1141

Answers (3)

Andy Rose
Andy Rose

Reputation: 16984

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") %>

Upvotes: 1

Lachlan Roche
Lachlan Roche

Reputation: 25946

Strongly Typed RedirectToAction is provided by the MvcContrib project.

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

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

Upvotes: 4

David Morton
David Morton

Reputation: 16505

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.

Upvotes: 1

Related Questions