ASP.NET MVC 2 - How do I use an Interface as the Type for a Strongly Typed View

Posted by Rake36 on Stack Overflow See other posts from Stack Overflow or by Rake36
Published on 2010-03-25T12:23:30Z Indexed on 2010/03/25 12:33 UTC
Read the original article Hit count: 284

Filed under:

I'd like to keep my concrete classes separate from my views. Without using strongly typed views, I'm fine. I just use a big parameter list in the controller method signatures and then use my service layer factory methods to create my concrete objects.

This is actually just fine with me, but it got me thinking and after a little playing, I realized it was literally impossible for a controller method to accept an interface as a method parameter - because it has no way of instantiating it. Can't create a strongly-typed view using an interface through the IDE either (which makes sense actually).

So my question. Is there some way to tell the controller how to instantiate the interface parameter using my service layer factory methods?

I'd like to convert from:

[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
[UrlRoute(Path = "Application/Edit/{id}")]
public ActionResult Edit(String id, String TypeCode, String TimeCode, String[] SelectedSchoolSystems,
            String PositionChoice1, String PositionChoice2, String PositionChoice3, String Reason, String LocationPreference,
            String AvailableDate, String RecipientsNotSelected, String RecipientsSelected) {

    //New blank app
    IApplication _application = ApplicationService.GetById(id);

to something like

[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
[UrlRoute(Path = "Application/Edit/{id}")]
public ActionResult Edit(String id, IApplication app) {

            //Don't need to do this anymore
            //IApplication _application = ApplicationService.GetById(id);

© Stack Overflow or respective owner

Related posts about asp.net-mvc