Detailed asp.net MVC dropdownlists

Source: Internet
Author: User

The dropdownlists in asp.net mvc seems to make a lot of confusion for programmers who start from asp.net. This article describes all the aspects you need to know in asp.net mvc in order to use dropdownlists.

Dropdownlist,combobox, no matter what you like to call them, they will be born into HTML select tags without exception. Between the <select> tab and the </select> closed tag, each list element must be wrapped around the <option> tag. Of course you can use the <optgroup> tab to logically into different groups. If the Value property is set for <option>, the Value property is the values of the Select element when the form is committed. And if you forget to assign a value to the Value property, then in the <option></option> label The contents of the inner parcel are the submitted values.

For the sake of simplicity, let me use a static list as an example, and you can add these as HTML directly to your view:

<select name="year">
  <option>2010</option>
  <option>2011</option>
  <option>2012</option>
  <option>2013</option>
  <option>2014</option>
  <option>2015</option>
</select>

Or, add a little bit of dynamic to the list, and if the year you want to list will automatically push back for 1 years as the New Year arrives:

<select name="year">
  <option><%= DateTime.Now.Year %></option>
  <option><%= DateTime.Now.AddYears(1).Year %></option>
  <option><%= DateTime.Now.AddYears(2).Year %></option>
  <option><%= DateTime.Now.AddYears(3).Year %></option>
  <option><%= DateTime.Now.AddYears(4).Year %></option>
  <option><%= DateTime.Now.AddYears(5).Year %></option>
</select>

It can be even simpler:

<select name="year">
  <% for (var i = 0; i < 6; i++){%>
   <option><%= DateTime.Now.AddYears(i).Year %></option>
  <%}%>
</select>

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.