A HtmlHelper method is extended in. NET MVC3 CheckBoxList

Source: Internet
Author: User

MVC has DropDownList method, very useful, but the most commonly used needs, a group of CheckBoxList how do not have a similar method? Depressed, do yourself a bar, directly on the code

 Public StaticMvchtmlstring CheckBoxList ( ThisHtmlHelper Helper,stringName, ienumerable<selectlistitem>selectlist) { returnCheckBoxList (helper, name, SelectList,New { }); }  Public StaticMvchtmlstring CheckBoxList ( ThisHtmlHelper Helper,stringName, Ienumerable<selectlistitem> SelectList,Objecthtmlattributes) {IDictionary<string,Object> htmlattributes =htmlhelper.anonymousobjecttohtmlattributes (htmlattributes); HashSet<string>Set=Newhashset<string>(); List<SelectListItem> list =NewList<selectlistitem>(); stringSelectedvalues = convert.tostring ((selectlist asselectlist). SelectedValue); if(!string. IsNullOrEmpty (selectedvalues)) {if(Selectedvalues.contains (",")) { string[] TempStr = Selectedvalues.split (',');  for(inti =0; i < tempstr.length; i++) { Set. ADD (Tempstr[i]); } } Else { Set. ADD (selectedvalues); } } foreach(SelectListItem Iteminchselectlist) {Item. Selected= (item. Value! =NULL) ?Set. Contains (item. Value):Set. Contains (item. Text); list. ADD (item); } selectlist=list; Htmlattributes.add ("type","checkbox"); Htmlattributes.add ("ID", name); Htmlattributes.add ("name", name); Htmlattributes.add ("style","margin:0 0 0 10px;line-height:30px; Vertical-align:-8px;border:none;"); StringBuilder StringBuilder=NewStringBuilder ();foreach(SelectListItem SelectIteminchselectlist) {IDictionary<string,Object> newhtmlattributes =htmlattributes.deepcopy (); Newhtmlattributes.add ("value", Selectitem.value); if(selectitem.selected) {Newhtmlattributes.add ("checked","checked"); } tagbuilder Tagbuilder=NewTagbuilder ("input"); Tagbuilder.mergeattributes<string,Object>(newhtmlattributes);stringInputallhtml=tagbuilder.tostring (tagrendermode.selfclosing); Stringbuilder.appendformat (@"<label style= "" margin:0 0 0 10px; "" > {0} {1}</label>", inputallhtml, Selectitem.text); } returnmvchtmlstring.create (stringbuilder.tostring ());} Private Staticidictionary<string,Object> Deepcopy ( Thisidictionary<string,Object>HT) {Dictionary<string,Object> _ht=Newdictionary<string,Object>(); foreach(varPinchHT) {_ht. ADD (P.key, P.value); } return_ht;} 

You can take it directly.

Each checkbox generated has a label outside it, feels good, does not like to be removed, and has a htmlattributes.add ("style", "margin:0 0 0 10px;line-height:30px; Vertical-align:-8px;border:none; "); To make the display look better, if it's not in your style, you can get rid of it.

A HtmlHelper method is extended in. NET MVC3 CheckBoxList

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.