如何在下拉列表中获取初始选定值

时间:2014-04-04 07:53:34

标签: asp.net asp.net-mvc

模型

        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%" })

如何在下拉列表中获取初始选定值并传递给某个函数以填充其他下拉列表。

2 个答案:

答案 0 :(得分: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%" })

答案 1 :(得分:0)

您必须在控制器的下拉列表中发送您想要选择的初始值。

与ddlLocation = {在加载时选择的选定值}一样 - 然后将在第一个下拉列表中选择该值。您可以使用相同的值将其发送到其他功能。

我希望这就是你要找的东西。如果没有,请忽略。

谢谢