Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC 2 - How do I use an Interface as the Type for a Strongly Typed View
    text
    copied!<p>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.</p> <p>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).</p> <p>So my question. Is there some way to tell the controller how to instantiate the interface parameter using my service layer factory methods?</p> <p>I'd like to convert from:</p> <pre><code>[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); </code></pre> <p>to something like</p> <pre><code>[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); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload