Use of ASP. NET MVC 3 CheckBoxList

Source: Internet
Author: User

You can use CheckBoxList directly in previous ASP. NET MVC, but later do not know what causes the CheckBoxList to be removed in MVC, so there is no checkboxlist in ASP. NET MVC 3 By default, so we cannot directly Use the @Html. CheckBoxList. Today we implement the CheckBoxList functionality in ASP. NET MVC 3 by ourselves.

First, let's look at the relationship between the two tables in my example, such as:

The Ztag table holds the article classification, the Tag field in the Zarticle table is classified as the "1,3,4" type, because an article may belong to several categories, such as it belongs to JQuery, CSS and so on.

The implementation of the CheckBoxList extension is given directly, as follows:

Public Static Mvchtmlstring CheckBoxList(This HtmlHelperHelper, StringName, IEnumerable<SelectListItem>Items){ VarStr= New StringBuilder();Str.Append(@"<div class=" "CheckBoxList" ">"); Foreach (VarItemInchItems) {Str.Append(@"<div class=" "List" "><input type=" "checkbox" "Name=" "");Str.Append(Name);Str.Append("\" Value=\ "");Str.Append(Item.Value);Str.Append("\""); If (Item.Selected)Str.Append(@"Checked=" "chekced" "" ");Str.Append("/>");Strappend (item. Text Str. Append "</div>" } Str. Append "</div>" return mvchtmlstring. Create (str. Tostring}             /span>                

It is not difficult to understand that the extended ASP. NET MVC HtmlHelper class has been used in this article in the ASP. NET MVC 3 page. Where we used the class SelectListItem that came with ASP. NET MVC 3, and then completed ienumerable<selectlistitem> initialization:

Public Static IEnumerable<SelectListItem> GetTags (ZarticleArticle, IEnumerable<Ztag>Tags){ VarResult= New List<SelectListItem> (); Foreach (VarTagInchTags) { VarItem= New SelectListItem { Text =Tag.Name, Value =Tag.Id.Tostring(), selected = Article.< Span class= "Typ" >tag. Split Contains (tag. Id. Tostring ()  }; Resultadd (item} return Result;}             

I put the method in the Common class, which passed the Zarticle object and the Ienumberable<ztag> object, in order to traverse ienumerable<ztag> to initialize ienumerable< The Selectlistitem> object so that the Ztag table field is output as a checkbox respectively. Finally, just call the GetTags () method, as in the article edit Action, as follows:

Public ActionResult Edit(StringId){ VarArticle=Db.Articles.Single(A=A.URLName ==Id); VarTags=Db.Tags.OrderByDescending(m => M. var tag =common. Gettags (article, Tags );  viewbag. Tags = Tag;return view (article< Span class= "pun"); }             /span>                

The last call in View is as follows:

@Html.  CheckBoxList("tag",(IEnumerable<selectlistitem>)ViewBag.  Tags)             

The results are as follows:

in ASP. NET MVC we want to remember the Don ' t do IT yourself principle, getting the CheckBoxList selected value is very simple, receive the check value method as follows:

Public ActionResult Edit(StringId, StringS, ZarticleArticle,String[]Tag){Article.URLName =Article.URLName; VarModel=Db.Articles.single (m => Murlname == Id Model. Tag = string. Join, Tag //article belongs to category  returnview (model               /span>                

Tag is the "tag" that we use when we call @Html. CheckBoxList, which is the "name" value of the CheckBox, which is the selected value of CheckBoxList, and then it is converted to a string using the Join method. Such as:

Here we implement the output of CheckBoxList in ASP. NET MVC 3, and successfully get to the CheckBoxList check value, hoping to bring a little help to a friend who uses ASP. NET MVC 3 but doesn't know how to use CheckBoxList.

Use of ASP. NET MVC 3 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.