[ASP. net mvc] My drop-down list Extension

Source: Internet
Author: User
I. Thinking

I previously read a blog article [ASP. NET MVC] About list control binding written by artech, which greatly benefited from simplifying the binding of "list control" Through htmlhelper extension.

However, we often encounter some problems in specific usage, that is, the data in the list is often not known in advance, or the data filled in the list is generally read from the database, what should we do at this time?

Ii. hypothetical scenarios

Now we want to build a simple news system, taking into account only the news and category entities. The following is a definition of news and categories:

/// <Summary> /// News Type /// </Summary> public class category {[displayname ("ID")] public int32 ID {Get; set ;} [displayname ("name")] Public String ofname {Get; set;} [displayname ("tag")] Public String label {Get; Set ;}} /// <summary> /// news /// </Summary> public class ournews {public guid ID {Get; set;} [displayname ("classification")] public int32 categoryid {Get; set;} [displayname ("title")] Public String title {Get; set;} [displayname ("content")] Public String content {Get; set ;}}

First, add some newstype data in advance and add it to contreller's contreller constructor. And add an "addnews" action:

Private list <Category> prevcategories; Public homecontroller () {prevcategories = new list <Category> () {new category {id = 0, ofname = "life ", label = "sh"}, new category {id = 1, ofname = "", label = "ty"}, new category {id = 2, ofname = "", label = "XX" }};} public actionresult addnews () {This. viewbag. prevcategories = This. prevcategories; // news category data, used to fill in the data in the drop-down list return view ();}

Then go to the view for adding news. When adding news, we generally make the column of classification into a drop-down list. What we do is to build a list <selectlistitem>, then directly call HTML. dropdownlist. However, HTML is often extended to keep the view clean. the dropdownlist method is shown in the view code below. 1 is a general method, and 2 is an extended method. The following code is written in two ways:

@ {Var prevcategories = This. viewbag. prevcategories as list <category >;}< Div class = "editor-label" >@html. labelfor (model => model. categoryid) </div> <Div class = "editor-field"> <Table> <tr> <TD> 1. normal stream </TD >@{ var items = new list <selectlistitem> (); foreach (VAR category in prevcategories) {items. add (New selectlistitem {text = category. ofname, value = category. id. tostring ()}) ;}}< TD> @ HTML. dropdownlist ("ID", items, "= select =") </TD> </tr> <TD rowspan = "2"> 2. extended stream </TD> <TD> @ (HTML. dropdownlist <Category> ("ID", "ofname", "ID", prevcategories, "= select =", new {})) </TD> @ * reflection * @ </tr> <TD> @ (HTML. dropdownlist <Category> ("ID", c => C. ofname, c => C. id. tostring (), prevcategories, "= select =", new {}) </TD> @ * delegate * @ </tr> </table> </div>

Let's take a look.

Dropdownlist extension uses two different methods, so there are two writing methods. Now we are starting to get started.

The disadvantage of the 1st method is that it seems redundant. Is it true that you need to build a list <selectlistitem> every time in this case. Of course this is not necessary. Use the second method to extend the dropdownlist.

Iii. Extended dropdownlist

The purpose of extended dropdownlist is to put the construction of list <selectlistitem> into the extension method instead of repeatedly constructing the list <selectlistitem> class, the problem encountered during the construction is how to obtain the data source type and fill fields. Because the data source type is variable, you need to use the generic type, the field filling problem occurs after the generic type is used. There are two solutions: Reflection and delegation.

Public static class dropdownlistextensions {// <summary> // reflection // </Summary> Public static mvchtmlstring dropdownlist <tentity> (this htmlhelper HTML, string name, string textfield, string valuefield, ilist <tentity> entitylist, string optionlabel, object htmlattributes) {var items = new list <selectlistitem> (); var type = typeof (tentity ); foreach (VAR entity in entitylist) {var textpropety = type. getproperty (textfield); var valuepropety = type. getproperty (valuefield); items. add (New selectlistitem {text = textpropety. getvalue (entity, null ). tostring (), value = valuepropety. getvalue (entity, null ). tostring ()});} return HTML. dropdownlist (name, items, optionlabel, htmlattributes );} /// <summary> /// delegate /// </Summary> Public static mvchtmlstring dropdownlist <tentity> (this htmlhelper HTML, string name, func <tentity, string> textfield, func <tentity, string> valuefield, ilist <tentity> entitylist, string optionlabel, object htmlattributes) {var items = new list <selectlistitem> (); foreach (VAR entity in entitylist) {items. add (New selectlistitem {text = textfield (entity), value = valuefield (entity)});} return HTML. dropdownlist (name, items, optionlabel, htmlattributes );}}
Iv. Summary

The reason is not very clear, but it is quite convenient to use it, so I will share it with you.

Sample program: Click here to download

 

Author: xanthondont

Source: http://www.cnblogs.com/hbq-fczzw/archive/2012/04/18/2455813.html

The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable.

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.