views:

53

answers:

2

I am currently running a website, i use a control that inherits from ITemplate to wrap all my usercontrols in.

basically what it is, is a table with a nice border, and i am able to dump anything in there, so if i change the container template, all the containers accross the site changes...

I am now in the process of rebuilding the entire application using MVC 2, does anyone know of a way i can achieve the same "Container" like template in MVC?

A: 

Hi Dusty,

There would be several ways to do this in MVC, but I would probably suggest using a custom HTML Helper.

There is an example on how to implement something similar in this post. Look for the section titled "Creating a DataGrid Helper" in that post for more information. I think you will be able to adapt that example to fit your purpose.

Chris Melinn
i have been trying to create some sort of an helper to accomplish this.if one looks at <% using (Html.BeginForm()) {%> CONTENT HERE<% } %>this basically wraps the content in a form.does anyone know how that works? all i need is to basically go<% using (Html.BeginContainer()) {%>CONTENT HERE<% } %>and i want that container to be a div of some sort, that has a class attached to it
Dusty Roberts
A: 

I managed Thanx here is an example:

public static class Block
{
    public static BlockHelper BeginBlock(this System.Web.Mvc.HtmlHelper content, string title)
    {
        return new BlockHelper(content, title);
    }
}

public class BlockHelper : IDisposable
{
    private readonly HtmlHelper _content;
    private readonly HtmlHelper _title;

    public BlockHelper(System.Web.Mvc.HtmlHelper content, string title)
    {
        _content = content;
        StringBuilder sb = new StringBuilder();
        sb.Append("<div>");
        sb.Append("<div><h1>" + title + "</h1></div>");
        _content.ViewContext.Writer.Write(sb.ToString());
    }

    public void Dispose()
    {
         StringBuilder sb = new StringBuilder();
        sb.Append("</div>");
        _content.ViewContext.Writer.Write(sb.ToString());
    }
}

and then i basically uses it as follows:

<% using( Html.BeginBlock("TITLE>CONTENT GOES HERE<%}%>
Dusty Roberts