在下拉列表中选择设定值 [英] Set selected value in dropdown list

查看:119
本文介绍了在下拉列表中选择设定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何设置一个下拉列表中选择的价值呢?以下是我迄今为止:

  @model Web.Models.PostGraduateModels.PlannedSpecialty 

@ Html.DropDownList(PlannedSpecialtyID)

//控制器
[HTTPGET]
公共PartialViewResult PlannedSpecialty()
{

//取得专业计划ID
变种pgtservice =新pgtService();
PostGraduateModels.PlannedSpecialty plannedSpecialty = pgtservice.GetPlannedSpecialtyId();


//获取数据的计划专业下拉列表从SpecialtyLookup
变种pgtServ =新PgtService();
变种项= pgtServ.GetPlannedSpecialtyDropDownItems();
ViewBag.PlannedSpecialtyId =物品;

返回PartialView(plannedSpecialty);


}

//服务
公开的IEnumerable< SelectListItem> GetPlannedSpecialtyDropDownItems()
{
使用(VAR DB = Step3Provider.CreateInstance())
{
VAR specialtyList = db.GetPlannedSpecialtyDdlItems();

返回specialtyList;

}

}

//数据访问
公开的IEnumerable< SelectListItem> GetPlannedSpecialtyDdlItems()
{

IEnumerable的<特殊和GT; 。特色= this._context.Specialties()GETALL();
变种selList =新的List< SelectListItem>();

的foreach(在专业VAR项)
{
变种tempps =新SelectListItem()
{
文本= item.Description,
值= item.Id.ToString()
};
selList.Add(tempps);
}


返回selList;
}


解决方案

我会建议你避免使用ViewBag /的ViewData /每周类型的代码。使用强类型的代码,并使它更具可读性。不要使用魔法字符串/魔术变量。我会集合属性添加到您的视图模型来保存选择列表的项目和另一个属性来保存所选项目的价值。

 公共类PlannedSpecialty 
{
公开的IEnumerable< SelectListItem> SpecialtyItems {集;获取;}
公众诠释SelectedSpeciality {集;获取;}

//其他属性
}

和在你的获取动作,如果你想设置一些项目的选择,

 公共PartialViewResult PlannedSpecialty()
{
变种pgtServ =新PgtService();
变种VM =新PlannedSpecialty();
vm.SpecialtyItems = pgtServ.GetPlannedSpecialtyDropDownItems();

//只是硬编码演示。你可能会得到一些源中的值。
vm.SelectedSpeciality = 25; //这里要设置选定的值。
返回查看(VM);
}

现在在视图中,使用 Html.DropDownListFor helper方法

  @ Html.DropDownListFor(X => x.SelectedSpeciality,Model.SpecialtyItems, 选择一项)


How do I set the selected value on a drop down list? Here is what I have so far:

@model Web.Models.PostGraduateModels.PlannedSpecialty

@Html.DropDownList("PlannedSpecialtyID")

//controller
        [HttpGet]
        public PartialViewResult PlannedSpecialty()
        {

            // Get Planned Specialty ID
            var pgtservice = new PgtService();
            PostGraduateModels.PlannedSpecialty plannedSpecialty = pgtservice.GetPlannedSpecialtyId();


           // Get Data for Planned Specialty DropDown List from SpecialtyLookup
            var pgtServ = new PgtService();
            var items = pgtServ.GetPlannedSpecialtyDropDownItems();
            ViewBag.PlannedSpecialtyId = items;

            return PartialView(plannedSpecialty);


        }

// service
        public IEnumerable<SelectListItem> GetPlannedSpecialtyDropDownItems ()
        {
            using (var db = Step3Provider.CreateInstance())
            {
                var specialtyList = db.GetPlannedSpecialtyDdlItems();

                return specialtyList;

            }

        }

// data access
        public IEnumerable<SelectListItem> GetPlannedSpecialtyDdlItems()
       {

            IEnumerable<Specialty> specialties = this._context.Specialties().GetAll();
            var selList = new List<SelectListItem>();

            foreach (var item in specialties)
            {
                var tempps = new SelectListItem()
                    {
                        Text = item.Description,
                        Value  = item.Id.ToString()
                    };
                selList.Add(tempps);
            }


            return selList;
       }

解决方案

I would recommend you to avoid using ViewBag/ViewData/ Weekly typed code. Use strongly typed code and it makes it more readable. Do not use the Magic strings/ Magic variables. I would add a collection property to your ViewModel to hold the SelectList items and another property to hold the selected item value.

public class PlannedSpecialty
{
   public IEnumerable<SelectListItem> SpecialtyItems { set;get;}
   public int SelectedSpeciality { set;get;}

  //Other Properties
}

and in your Get action, If you want to set some Item as selected,

public PartialViewResult PlannedSpecialty()
{ 
    var pgtServ = new PgtService();
    var vm=new PlannedSpecialty();
    vm.SpecialtyItems = pgtServ.GetPlannedSpecialtyDropDownItems();    

   //just hard coding for demo. you may get the value from some source.  
    vm.SelectedSpeciality=25;//  here you are setting the selected value.
   return View(vm);
}

Now in the View, use the Html.DropDownListFor helper method

@Html.DropDownListFor(x=>x.SelectedSpeciality,Model.SpecialtyItems,"select one ")

这篇关于在下拉列表中选择设定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆