tags:

views:

37

answers:

2

In the CRUD areas of my MVC app I have lots of seemingly pointless view files, such as:

<%@ Page Title="" Language="C#" MasterPageFile="Some.Master" Inherits="System.Web.Mvc.ViewPage<SomeModel>" %>

<asp:Content ID="ContentID" ContentPlaceHolderID="SomePlaceHolder" runat="server">
   <%= Html.DisplayForModel() %>
</asp:Content>

This is of course pretty unDRY.

Is it possible to use a shared view for this while at the same time preserving the Strong Typing? (e.g. by specifying the generic type in the controller?)

+1  A: 

If it is pretty common you could place this code in the master file (Html.DisplayForModel()) so that you don't need to override it in each view. And only for the views that don't need this common behavior you could override it.

Darin Dimitrov
But then I'm still left with the strongly typed view stubs I think. How to get rid of those?
UpTheCreek
You could create a shared view which your controller actions will `return View("~/Views/Shared/SomeSharedView.aspx", someModel);`
Darin Dimitrov
But I suppose you loose the strong typing then? Perhaps it doesn't matter...?
UpTheCreek
The strong typing makes sense only when you are working with a specific view and a specific view model. If you have common functionality such as `Html.EditorForModel` and `Html.DisplayForModel` you could easily put this inside the master page.
Darin Dimitrov
+1  A: 

Instead of having a display view for each CRUD controller you can have a single Display.aspx in /Views/Shared/. The view engine searches /Views/{yourcontroller} and then /Views/Shared/ for whatever its looking for.

If all your view is <%= Html.DisplayForModel() %> then there is no need for strongly typing. DisplayForModel() knows how to figure that out.

FOr more information on this technique: http://haacked.com/archive/2009/08/04/default-templated-views.aspx

jfar
Thanks for the link - hadn't read that article.
UpTheCreek