ASP. net mvc allows @ Html. DropDownList to display the default value,

Source: Internet
Author: User

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.

 

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.