ASP. net mvc allows @ Html. DropDownList to display the default value,
@ Html. during the DropDownList process, it is found that its usage is very limited. For example, when loading, display the Set Default item or adjust its display style, I checked some information on the Internet, the problem was finally solved.
I. View code
@ Using (Ajax. beginForm ("Edit", new AjaxOptions () {OnSuccess = "afterEdit"}) {@ Html. hiddenFor (model => model. ID) @ Html. hiddenFor (model => model. isUsed) <div style = "padding: 40px 40px 30px 50px;"> <table style = "font-size: 12px "align =" center "> <tr> <td> @ Html. label ("comment title:") </td> <td> @ Html. dropDownList ("YzPositionCriticsID", ViewData ["CriticsPositionType"] as SelectList) </td> </tr> <td> @ Html. label ("comment title:") </td> <td> @ Html. dropDownList ("YzPositionEvaluationID", ViewData ["EvaluationPosition"] as SelectList) </td> </tr> <td> @ Html. label ("Weight:") </td> <td> @ Html. textBoxFor (model => model. weight) </td> </tr> </table> </div>}
2. Controller code
Public ActionResult Edit (string id) {// 6.1 check the Guid Gid = new Guid (ID); Model from the database based on the id. DTO. yzWeightEntityDTO weight = weightBLL. loadEnities (u => u. ID = Gid ). firstOrDefault (). toDto (); // 6.2 query the data List of the YzPositionEntity object <Model. DTO. yzPositionEntityDTO> positionList = positionBLL. loadEnities (). toList (). select (s => s. toDto ()). toList (); // 6.3 encapsulate the data of YzPositionEntity into SelectList, and specify the value and text attribute SelectList selList1 = new SelectList (positionList, "ID ", "PositionType", weight. yzPositionCriticsID); SelectList selList2 = new SelectList (positionList, "ID", "PositionType", weight. yzPositionEvaluationID); // 6.4 place the generated set in ViewData ["CriticsPositionType"] = selList1; ViewData ["EvaluationPosition"] = selList2; return View (weight );}
Note: SelectList selList1 = new SelectList (list, "", "", ***). Here, ** is the default value to be displayed when the drop-down box is loaded.
The final display effect is as follows:
Summary:
In the process of learning MVC, there will always be some small problems from time to time, and you need to turn around to find the answer. In the process of solving the problem, experience is really important. It can tell you where the breakthrough point of a problem is. In addition, skilled use of API documentation is also an important way to solve the problem. Finally, it is found that solving the problem is not the motivation to increase your learning interest.