How to use dropdownlistfor in asp.net mvc

Source: Internet
Author: User

In asp.net mvc, you can use the Dropdownlistfor method to allow users to select a number in a list that has been set. The usage is not complex, here simply makes a record.

First we want to define a Model, the user selects the specified value in the DropDownList to assign to the attribute Readytimehour

public class Ericsunmodel
{public
    string Readytimehour {get; set;}
}

After the model is defined, the logic of the controller is then processed

"Note: Here is a viewdata to record all the list values to be displayed in the DropDownList."

Public ActionResult ericsunaction ()
{
    Ericsunmodel Esmodel = new Ericsunmodel ();
    Esmodel.readytimehour = ";
    
    " Generatereadytimeviewdata ();
    
    Return View (Esmodel);
}
    
private void Generatereadytimeviewdata ()
{
    viewdata["hourlist"] = Gettimehourlist ();
}
    
Private list<selectlistitem> gettimehourlist ()
{
    list<selectlistitem> hourlist = new List< Selectlistitem> ();
    
    for (int i = 0; i < i++)
    {
        if (I <)
        {
            Hourlist.add (new SelectListItem {Text = "0" + i.to String (), Value = "0" + i.tostring ()});
        else
        {
            Hourlist.add (new SelectListItem {Text = i.ToString (), Value = i.ToString ()});
        }
    
    return hourlist;
}

Next we can use the following line of code in the View to bind DropDownList

"NOTE: The first parameter is the property in the binding Model , that is,--> to assign a value to this property"

"NOTE: The second parameter is DropDownList for all data sources"

@Html. Dropdownlistfor (M => m.readytimehour, viewdata["Hourlist"] as list<selectlistitem>)

The screenshot below shows

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/aspx/

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.