MVC Helper Methods, mvchelpermethods

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.