See helper form methods. They provide syntax as follows:
<% using (Html.BeginForm()) { %> <p>Form contents go here.</p> <% } %>
The template for implementing such HTML helpers is somewhat more active than regular helpers such as "just return the HTML string." Basically, your helper method will be Response.Write opening tag (s) when it is called, and returns some custom object that implements IDisposable . When the return value is located, it should Response.Write close the tag (s).
Here is a working example:
public static MyContainer WrapThis(this HtmlHelper html) { html.ViewContext.HttpContext.Response.Write("<div><div>"); return new MyContainer(html.ViewContext.HttpContext.Response); } public class MyContainer : IDisposable { readonly HttpResponseBase _httpResponse; bool _disposed; public MyContainer(HttpResponseBase httpResponse) { _httpResponse = httpResponse; } public void Dispose() { if (!_disposed) { _disposed = true; _httpResponse.Write("</div></div>"); } GC.SuppressFinalize(this); } }
This will allow you to rewrite your opinion on the following:
<% using (Html.WrapThis()) { %> <h3>Profiles</h3> <div class="divOption"> <%= Html.ActionLinkWithImage("Create new", "add.png", Keys.Actions.CreateProfile, "add")%> </div> <% } %>
source share