pbz
pbz

Reputation: 9085

ASP.NET MVC grouping / reusing view functionality

On a given page, to let's say to display a window, I may need some JavaScript initialization, some DIVs with certain IDs and it all needs to be linked with each other. The JavaScript may need to know about a DIV's ID, the DIVs may need to be nested in a certain way and so on. Obviously I don't want to worry about this on every single page I use that code. That would make it error-prone and not DRY.

With WebForms I would solve this problem by writing a custom control. How are you guys doing this in your projects? Are you using extension methods spitting out strings, maybe user controls? What's the recommended way?

Thanks.

EDIT:

Here's an example when using master-pages:

  1. In the HEAD content region I would need some jQuery code that sets up some functionality
  2. In one content region I would put some HTML required to show a part of the window
  3. In a different content region I would put the actual HTML that's displayed in the window.

So all these 3 pieces would require 3 different blocks of code but would be logically linked.

EDIT 2

Code example (using a master-page)

<asp:Content ContentPlaceHolderID="HeaderContent" runat="server">
   <script type="text/javascript">
     $(document).ready(function() { DoSomething('div1', 'div2'); });
   </script>
</asp:Content>

<asp:Content ContentPlaceHolderID="TopContent" runat="server">
  <div id='div1'> ... </div>
</asp:Content>

<asp:Content ContentPlaceHolderID="BottomContent" runat="server">
  <div id='div2'> ... </div>
</asp:Content>

div1 and div2 are coupled with the JavaScript function call. If I make a mistake, give the DIV a different ID, the code would break. This is a simple example that proves the point. It gets a lot more complicated when the coupling relies on div1 having a certain structure for example.

Upvotes: 0

Views: 192

Answers (2)

Mathias F
Mathias F

Reputation: 15891

The closest you can get to CustomControlls are Partials. They are not as powerfull as CustomControlls because they have no eventhandling (obviously) but are a good way to separate common html.

Upvotes: 1

David Andres
David Andres

Reputation: 31781

Can you leverage Site.Master for this purpose, or at least another Master of your own creation? All of your views can then reference this Master and gain the benefit of initialized JavaScript and layout elements.

To me, this makes sense if the content is static in nature. If not, I'd go with a custom Html.Init helper method.

I'm envisioning something like the following:

global.js

function doSomething()
{
}

//more code here

master page

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>

<!DOCTYPE html PUBLIC 
 "-//W3C//DTD XHTML 1.0 Strict//EN" 
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>
      <asp:ContentPlaceHolder ID="TitleContent" runat="server" />
    </title>
    <link rel="Stylesheet" href="~/Content/styles.css" />
    <script type="text/javascript" src="global.js"></script>
</head>
<body>
    <div id="customContent">
    </div>
    <div id="content">
        <asp:ContentPlaceHolder ID="MainContent" runat="server" />
    </div>    
</body>
</html>

Upvotes: 2

Related Questions