ASP.NET MVC 讓@Html.DropDownList顯示預設值

來源:互聯網
上載者:User

標籤:bar   turn   ajax   文檔   target   keyword   相關   ***   12px   

          在使用@Html.DropDownList的過程中,發現它的用法很局限,比如在載入的時候顯示設定的預設項或者調整它的顯示樣式,在網上查了一些資料,終於把這個問題解決了。

 

    一、View代碼

        

 

[html] view plain copy  
  1. @using (Ajax.BeginForm("Edit", new AjaxOptions() { OnSuccess = "afterEdit" }))  
  2. {  
  3.     @Html.HiddenFor(model => model.ID)  
  4.     @Html.HiddenFor(model => model.isUsed)  
  5.   
  6. <div style="padding: 40px 40px 30px 50px;">  
  7.     <table style="font-size:12px" align="center">  
  8.         <tr>  
  9.             <td>@Html.Label("評論人職務:")</td>  
  10.             <td>@Html.DropDownList("YzPositionCriticsID", ViewData["CriticsPositionType"] as SelectList)</td>  
  11.         </tr>  
  12.         <tr>  
  13.             <td>@Html.Label("被評論人職務:")</td>  
  14.             <td>@Html.DropDownList("YzPositionEvaluationID", ViewData["EvaluationPosition"] as SelectList)</td>  
  15.         </tr>  
  16.         <tr>  
  17.             <td>@Html.Label("權重:")</td>  
  18.             <td>@Html.TextBoxFor(model => model.Weight)</td>  
  19.         </tr>  
  20.     </table>  
  21. </div>  
  22.   
  23. }  

 

 

    二、Controller代碼    

        

 

[csharp] view plain copy  
  1. public ActionResult Edit(string id)  
  2. {  
  3.     //6.1根據ID從資料庫中查出要修改的相關資料  
  4.     Guid Gid = new Guid(id);  
  5.     Model.DTO.YzWeightEntityDTO weight = weightBLL.LoadEnities(u => u.ID == Gid).FirstOrDefault().ToDto();  
  6.   
  7.     //6.2查詢YzPositionEntity實體的資料  
  8.     List<Model.DTO.YzPositionEntityDTO> positionList = positionBLL.LoadEnities().ToList().Select(s => s.ToDto()).ToList();  
  9.   
  10.     //6.3將YzPositionEntity的資料封裝到 SelectList中,制定要產生下拉框選項的value和text屬性  
  11.     SelectList selList1 = new SelectList(positionList, "ID", "PositionType", weight.YzPositionCriticsID);  
  12.     SelectList selList2 = new SelectList(positionList, "ID", "PositionType",weight.YzPositionEvaluationID);           
  13.     
  14.     //6.4把產生的集合放到ViewData中  
  15.     ViewData["CriticsPositionType"] = selList1;  
  16.     ViewData["EvaluationPosition"] = selList2;  
  17.     return View(weight);  
  18. }   

 

 

     在這裡需要注意的是:SelectList selList1 = new SelectList(list,"","",***),這裡的***就是下拉框載入時候要顯示的預設值。

     

     最後的顯示效果如下:

     

    

     總結:

     在MVC的學習過程中,總會時不時遇到一些小問題,需要轉個彎才能找到答案。在解決問題的過程中經驗真的很重要,它能告訴你一個問題的突破點在什麼地方;除此之外,熟練的使用API文檔也是解決問題的重要途徑;最後,真的發現解決問題是提高自己學習興趣的不接動力。

 

ASP.NET MVC 讓@Html.DropDownList顯示預設值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.