ASP.Net MVC Custom Controls - Container

Is there a way to make a helper in Asp.Net MVC to migrate another html like this:

<div class="lightGreyBar_left"> <div class="lightGreyBar_right"> <!--Content--> <h3> Profiles</h3> <div class="divOption"> <%= Html.ActionLinkWithImage("Create new", "add.png", Keys.Actions.CreateProfile, "add")%> </div> <!--Content--> </div> </div> 

Thus, the helper will display the contents of the div and the content that is passed to the helper method as a parameter.

+4
source share
1 answer

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> <% } %> 
+4
source

Source: https://habr.com/ru/post/1310707/


All Articles