问题
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?
回答1:
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.
回答2:
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<%}%>
来源:https://stackoverflow.com/questions/2231501/replacement-for-itemplate-in-mvc