MVC4 Custom HtmlHelper

Source: Internet
Author: User

Haven't written a blog for a long time, recently only read blog not write habits very bad ah.

Well, most recent projects have used a lot of forms, many forms have special writing, but there are a lot of duplication in the project, this time if can be encapsulated into htmlhelper will greatly reduce the workload.
Here's a basic usage model, Memo

Using system;using system.collections.generic;using system.linq;using system.linq.expressions;using System.Web;        After the using system.web.mvc;//introduces the following namespaces, it is possible to come out of the view directly @ namespace system.web.mvc{public static class Formhtmlhelper {        Private Const string editorwidth = "100";       Private Const string editorheight = "100"; Give the following method the first argument to this HtmlHelper helper, so you can @ht in//points out, otherwise you have to @[the custom class]. [Your method]. The following method seems to need to pass in two values,//actually as long as the @html. You can order it out. public static mvchtmlstring Necessarylabeler (this htmlhelper helper,st            Ring name) {var ntag = new Tagbuilder ("span"); Ntag.            addCssClass ("Red"); Ntag.            Setinnertext ("*");            var nametag = new Tagbuilder ("span"); Tag.            addCssClass (""); NameTag.            Setinnertext (name); return new Mvchtmlstring (Ntag. ToString () +nametag.        ToString ());        }//The following method can pass the model of the view in, get the value of the method to see the following LAMDA expression ... public static mvchtmlstring Disablededitorfor<tmodel, TValue> (this htmlhelper<tmodel> html, Expression<func<tmodel, tvalue>> Expression) {Obje CT data = Modelmetadata.fromlambdaexpression<tmodel, tvalue> (expression, HTML. ViewData).            Model;            if (data = = null) {data= "";        }//to do want! }            }}

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.