when using WebForm, the drop-down box can only be used to bind the code directly in the background. Now let's take a look at the DropDownList in MVC if and accept the values passed from the controller.
the first type: Using DropDownList
Controller code:
public ActionResult Index () {//1.1 query yzseriesentity data list<model.yzseriesentity> serieslist = Seriesbll.loadenities (). ToList (); 1.2 Encapsulate the yzseriesentity data into SelectList, and set the value and Text property to generate the drop-down box option selectlist selList1 = new SelectList (serieslist, "Serialname", "serialname"); 2.1 Query yzdivisionentity data list<model.yzdivisionentity> divisionlist = Divisionbll.loadenities (). ToList (); 2.2 The yzdivisionentity data is encapsulated into the selectlist, and the value and Text property to generate the drop-down box option selectlist selList2 = new SelectList (divisionl ist, "Divisionname", "divisionname"); 3. Call SelectList as method, automatically generate SelectListItem collection, and deposit in ViewBag Viewbag.sellist1 = sellist1.asenumerable (); Viewbag.sellist2 = Sellist2.asenumerable (); return View (); }
View Code:
<!--------------Add dialog box--------------> <div id= "Adddiv" > @using (Ajax.beginform ("Add" , new Ajaxoptions () {onsuccess = "Afteradd"})) {<table> <tr> <td> Ref.:</td> <td> @Html. TextBox ( "StaffID") </td> </tr> <tr> <td> name:</td> <td> @Html. TextBox ("Staffname") </td> </tr> <tr> <td> Gender:</td> <TD&G T <input type= "Radio" id= "Genderm" name= "Sex" value= "male"/> Male <input type= "Radio" id= "Gend ErF "Name=" Sex "value=" female "checked/> women </td> </tr> <tr> <td> Series:</td> <td> @Html. DropDownList ("Serialname", Viewbag.sellist1 as Ienumerable<selectlistitem>) & lt;/td> </tr> <tr> <td> department or grade group: </TD> ; <td> @Html. DropDownList ("Divisionname", Viewbag.sellist2 as Ienumerable<selectlistitem >) </td> </tr> <tr> <td> subject:</td> <td> @Html. TextBox ("subjects") </td> </tr> <tr> <TD&G t; appointment date:</td> <td> @Html. TextBox ("Engagedate") </td> </tr> <tr> <td> participation in work dates :</td> <td> @Html. TextBox ("Workdate") </td> </tr> <tr> <td> title:</td> <td> @Html. TextBox ("Jobqualification") < /TD> </tr> <tr> <td> Social Security Number:</td> <td> @Html. TextBox ("Identitycard") </td> </tr> </table>} </div>
Effect Display:
The second type: Use <select></select>
View Code:
<!--Select weights --<div> <span> @Html. Label ("Select Weight:") </span> <span> < Select Id= "CC" class= "Easyui-combobox" name= "dept" data-options= "Valuefield: ' id ', TextField: ' Weight ', url: '/ Settingevaluation/listoption ' "/> </span> </div>
Controller code:
The dropdown box corresponds to the list of public ActionResult listoption () { //2.1. Query out the weight entity and turn it into a dto type list< model.dto.yzweightentitydto> weightlist = weightbll.loadenities (). ToList (). Select (s = = S.todto ()). ToList (); 2.2 Return JSON return JSON (Weightlist, jsonrequestbehavior.allowget);
Effect Display:
Summarize:
Comparison of two modes of transmission:
The first is that the controller passes through the ViewBag value, the foreground receives through the @html.dropdownlist, the second is through the JSON value, the foreground through the URL binding Valuefield and the TextField value to obtain the data. There is not much difference between the two, but because the values used <select></select> received, using HTML tags, it can also be used to bind other JS events, so if there is a function required, the latter is more flexible than the former.
Two ways to transfer values from the ASP. NET MVC drop-down box