http://www.cnsendblog.com/index.php/?p=137
GPS platform, website construction, software development, system operation and maintenance, find Senda network technology!
http://cnsendnet.taobao.com
In the past, when WebForm was used, the drop-down box only needed to bind the code directly in the background. Now let's take a look at the DropDownList in MVC if and accept the value passed from the Controller.
First: use DropDownList Controller code:
1. public ActionResult Index() 2. { 3. 4. //1.1 querying data of YzSeriesEntity 5. List<Model.YzSeriesEntity> seriesList = seriesBLL.LoadEnities().ToList(); 6. //1.2 encapsulate the data of YzSeriesEntity into the SelectList, and specify the value and text properties of the dropdown box options to be generated 7. SelectList selList1 = new SelectList(seriesList, "SerialName", "SerialName"); 8. 9. //2.1 querying data of YzDivisionEntity 10. List<Model.YzDivisionEntity> divisionList = divisionBLL.LoadEnities().ToList(); 11. //2.2 the data of YzDivisionEntity is encapsulated in the SelectList, and the value and text properties of the dropdown box options to be generated are specified 12. SelectList selList2 = new SelectList(divisionList, "DivisionName", "DivisionName"); 13. 14. //3. Call the As method of Selectlist to automatically generate the SelectListItem collection and store it in ViewBag 15. ViewBag.selList1 = selList1.AsEnumerable(); 16. ViewBag.selList2 = selList2.AsEnumerable(); 17. 18. return View(); 19. }
View code:
1. <!-------------- add dialog box--------------> 2. <div id="addDiv"> 3. @using (Ajax.BeginForm("Add", new AjaxOptions() { OnSuccess = "afterAdd" })) 4. { 5. <table> 6. <tr> 7. <td>Serial number:</td> 8. <td> 9. @Html.TextBox("StaffID") 10. </td> 11. </tr> 12. <tr> 13. <td>Full name:</td> 14. <td>@Html.TextBox("StaffName")</td> 15. </tr> 16. <tr> 17. <td>Gender:</td> 18. <td> 19. <input type="radio" id="GenderM" name="Sex" value="male" />male 20. <input type="radio" id="GenderF" name="Sex" value="female" checked />female 21. </td> 22. </tr> 23. <tr> 24. <td>Series:</td> 25. <td> 26. @Html.DropDownList("SerialName", ViewBag.selList1 as IEnumerable<SelectListItem>) 27. </td> 28. </tr> 29. <tr> 30. <td>Department or grade group:</td> 31. <td> 32. @Html.DropDownList("DivisionName", ViewBag.selList2 as IEnumerable<SelectListItem>) 33. </td> 34. </tr> 35. <tr> 36. <td>Subjects:</td> 37. <td> 38. @Html.TextBox("Subjects") 39. </td> 40. </tr> 41. <tr> 42. <td>Date of appointment:</td> 43. <td> 44. @Html.TextBox("EngageDate") 45. </td> 46. </tr> 47. <tr> 48. <td>Working date:</td> 49. <td> 50. @Html.TextBox("WorkDate") 51. </td> 52. </tr> 53. <tr> 54. <td>Title:</td> 55. <td> 56. @Html.TextBox("jobQualification") 57. </td> 58. </tr> 59. <tr> 60. <td>ID number:</td> 61. <td> 62. @Html.TextBox("IdentityCard") 63. </td> 64. </tr> 65. 66. </table> 67. } 68. 69. </div>
Effect display:
Second, use < Select > < Select >
View code:
1. <!--Selection weight--> 2. <div> 3. <span>@Html.Label("Please select the weight:")</span> 4. <span> 5. <select id="cc" class="easyui-combobox" name="dept" 6. data-options="valueField:'ID',textField:'Weight',url:'/SettingEvaluation/ListOption'" /> 7. </span> 8. </div>
Controller code:
- //Drop down list
- public ActionResult ListOption()
- {
- //2.1. Query the weight entity and convert it to 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:
Conclusion:
Comparison of two methods of value transmission:
The first is that the controller transmits values through ViewBag, and the foreground receives values through @ Html.DropDownList; the second is that the foreground transmits values through Json, and the foreground obtains data by binding the values of valueField and textField through url. There is no big difference between the two. However, because the value passed is received by < Select > < Select > and HTML tags are used, it can also be used to bind other js events. If there is a functional need, the latter is more flexible than the former.
http://www.cnsendblog.com/index.php/?p=137
GPS platform, website construction, software development, system operation and maintenance, find Senda network technology!
http://cnsendnet.taobao.com