MVC Helper Methods, mvchelpermethods
In. net MVC, for example, @ Url. action () @ Url. conyent () @ Html. displayfor () and so on are the Helper Methods help Methods we will talk about here. What are they doing? where are their main purposes? They are mainly used to generate html tags and code under certain logic.
Embedded helper methodsThe preceding helper methods is a built-in help method of the mvc framework. We can also create our own help methods, which can be divided into two types: Embedded external forms. The method for creating an embedded method is similar to that for a common C # method, but there is a difference. The embedded helper methods method does not return values. They directly output the label results to the response stream. The output result will be output in bold. The advantage of using helper Methods is that there are many duplicate labels and Code on the view page. I personally do not like it. I would rather write more labels than wasting my brain capacity. But I still need to know this writing method.
External Helper MethodsThe external help method is to extend the HtmlHelper class in the framework and Other helper classes to return MVCHtmlString, Which is parsed by the framework itself, that is, some extension methods written for these classes.
public static class CustomHelpers { public static MvcHtmlString ListArrayItems(this HtmlHelper html, string[] list) { TagBuilder tag = new TagBuilder ("ul" ); foreach (string str in list) { TagBuilder itemTag = new TagBuilder ("li" ); itemTag.SetInnerText(str); tag.InnerHtml += itemTag.ToString(); } return new MvcHtmlString (tag.ToString()); } }
Here we use TagBuilder to concatenate an html string and return an MVCHtmlString object. @ Using can be used to add a reference. @ Html. listArrayItems (string []) ViewBag. fruits) is also very simple. This seems to be quite useful than embedded helper. Here we will introduce the basic origins of. net mvc helper Methods. If you like it, please recommend it. Thank you n (* too many minutes *) n