2014-04-04 24 views
0

模型得到的初始設定值如何在下拉列表

 public class SModel 
     { 
      public string ddlLocation { get; set; } 
      public IEnumerable<SelectListItem> locationItems { get; set; } 
     } 

控制器

 [HttpGet] 
    public ActionResult StudyScheduler(SModel model) 
    { 
     model.spLocationItems = FillLocation(); 
     //Here i have to fill other dropdownlist on selected value of location. 
     return View(); 
    } 

    private List<SelectListItem> FillLocation() 
    { 
     List<SelectListItem> items = new List<SelectListItem>(); 
     //Fill items from DB 
      return items; 
    } 

查看

@Html.DropDownListFor(x => x.ddlLocation, new SelectList(Model.locationItems, "Value", "Text"), new { @class = "cssTextbox", style = "width:90%" }) 

如何在下拉列表獲取一個初始值選擇並傳遞給一些函數來填充其他下拉列表。

+0

你想獲得初始選擇的值在哪裏,在控制器中,在客戶端腳本還是...? – Laziale

+0

在控制器 – John

+0

第一次ddl填充後立即 – John

回答

0

您可以使用SelectList()的第4個參數。

new SelectList(items, "value", "text", "please select"); 

所以,你的代碼將

 @Html.DropDownListFor(x => x.ddlLocation, 
    new SelectList(Model.locationItems, "Value", "Text","please select"), 
new { @class = "cssTextbox", style = "width:90%" }) 
+0

Ashwini want在第一次ddl填充之後立即獲得控制器中的初始選定值。 – John

0

你必須發送你想從控制器的下拉菜單,選擇初始值。

像ddlLocation = {選擇加載時選擇的值} - 然後將在第一個下拉列表中選擇該值。您可以使用相同的值將其發送給其他功能。

我希望這是你在找什麼。如果不是,請忽略。

謝謝

+0

我如何獲得選定的價值 – John