Use of Html. DropDownList in Asp.net MVC5,

Source: Internet
Author: User
Tags asp net

Use of Html. DropDownList in Asp.net MVC5,

I. Binding static drop-down list items

Bind static items to the drop-down list. We can use the SelectListItem set as the drop-down list of the data source.
1     @Html.DropDownList("dropRoles", new List<SelectListItem>()2     {3         new SelectListItem() { Text= "Yes", Value = "true" },4         new SelectListItem() { Text= "No", Value = "false", Selected = true }5     }, "Select ..."),new { @class = "myClass", style = "width: 250px;" })

In the above code snippet,

The first parameter is the name in the drop-down list;

The second parameter is the set in the drop-down list of the SelectListItem to be used as the data source and the third parameter is the default value. If "select = true" is selected, it is not specified in any SelectListItem.

The third parameter is the default null item displayed in the drop-down list.

The fourth parameter is to write the style/class or other HTML attributes (htmlObjectAttributes) in the drop-down list ).

2. bind multiple table data

@ Html. DropDownList ("MultipleTables", ViewBag. Roles as SelectList)

Controller operation method code

var data = from p in db.PersonalDetails                       join f in db.Files                       on p.AutoId equals f.PersonalDetailsId                       select new                        {                           PersonName = p.FirstName,                           MyFileName = f.FileName                       };            SelectList list = new SelectList(data, "MyFileName", "PersonName");            ViewBag.Roles = list;

 

In the code snippet above, we added two tables to use LINQ to form objects with the PersonName and MyFileName attributes. Convert dataTextFileld And dataValueField . In this case, the set is set to ViewBag. Roles, which is used as the view in the drop-down list of the data source.

3. Bind a model to the drop-down list

Bind a model attribute to the drop-down list and use the following code snippet.
public IEnumerable<SelectListItem> Categories { get; set; }
Set this attribute value in the operation method of the controller,
model.Categories = db.CategoryModels.Where(c => c.SectionId == sectionId && c.Active == true).OrderBy(ct => ct.CategoryName).ToList().Select(x => new SelectListItem { Value = x.CategoryId.ToString(), Text = x.CategoryName }).ToList();
In the code snippet above, note that in the code of the third line, all the projects selected from the list are created SelectListItem The value specified by the object is category id and category name as text. When this set is bounded in the drop-down list, the category id serves as the item value and category name as the text of the drop-down list item. 4. Change the resend of the drop-down list item in the form of ASP. net mvc and change the drop-down list item to automatically send back. We can specify the onchange attribute in htmlObjectAttributes.
@Html.DropDownList("MultipleTables", ViewBag.Roles as SelectList, "Select ...", new { @class = "myClass", style = "width: 250px;", onchange = "this.form.submit();" })
In the preceding code snippet, The onchange attribute forces the form to be submitted to the server. When the user changes the selected content in the drop-down list. For more information, see
How to add events to DropDownList in asp net mvc Framework

WebForm server events cannot be used in MVC, but only one Action can be called in Ajax mode.

Problems with obtaining htmldropdownlist in ASPNET MVC2

Get the value through Jquery and assign it to a hidden control.
$ ("# DropDownList ID"). change (function (){
Var selec = $ (this). val ();
// Assign selec to the hidden Control
});

In the Action, you can directly obtain the value of the hidden Control ID.
 

Related Article

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.