Mvc 3:填充下拉框

时间:2013-09-24 19:47:27

标签: asp.net-mvc-3

我知道这很简单但对Mvc来说是新手。我想用数据填充下拉框 从GetDescription方法,以便下拉列表显示描述,并在选择时,代码作为值传递。

如何编写html片段?

 public class HomeController : Controller
                {
                    public ActionResult Index()
                    {


                        return View();
                    }

                    public static List<Populate> GetDescription()
                    {
                        List<Populate> myInfor = new List<Populate>()
                        {
                            new Populate{Id=1,Description="Lagos", Code="lag"},
                            new Populate{Id=2,Description="Ibadan", Code="lba"},
                            new Populate{Id=3,Description="Akure", Code="Aku"},
                            new Populate{Id=4,Description="Ejigbo", Code="Eji"},
                            new Populate{Id=5,Description="Oshogbo", Code="Osh"}
                        };

                        return myInfor;
                    }

                }


                public class Populate
                {
                    public int Id { get; set; }
                    public string Description { get; set; }
                    public string Code { get; set; }
                }


                Here is the html
                *********************

                @model PopulateDropDownList.Models.Populate

                @{
                    ViewBag.Title = "Index";
                }

                <h2>Index</h2>



                <p>


                @Html.DropDownList(Model.id, Model.Description, "--Select One--")

                </p>

2 个答案:

答案 0 :(得分:2)

您也可以这样做。

修改你的代码:

    public static class Helper
{
                public static List<SelectListItem> GetDescription()
                {
                    List<Populate> myInfor = new List<Populate>()
                    {
                        new Populate{Id=1,Description="Lagos", Code="lag"},
                        new Populate{Id=2,Description="Ibadan", Code="lba"},
                        new Populate{Id=3,Description="Akure", Code="Aku"},
                        new Populate{Id=4,Description="Ejigbo", Code="Eji"},
                        new Populate{Id=5,Description="Oshogbo", Code="Osh"}
                    };

                     var result = new List<SelectListItem>(); 
                      var items = from n in myInfor
                            select new SelectListItem
                            {
                                Text = n.Description,
                                Value = n.Id.toString()
                            };
                foreach (var item in items)
                    result.Add(item);

                    return result;
                }
}

比你的观点: @using YourProject.Helper

@Html.DropDownList("AnyTextAsID", Helper.GetDescription()) 

答案 1 :(得分:0)

您可以在视图中执行此操作:

@{
    Dictionary<string, string> populate = new Dictionary<string, string>();
        populate.Add("Lagos", "lag");
        populate.Add("Ibadan", "lba");
        populate.Add("Akure", "aku");
        populate.Add("Ejigbo", "eji");
        populate.Add("Oshogbo", "osh");
}
@Html.DropDownList("myinfo", new SelectList(populate.ToList(), "Value", "Key", "Lagos"), new { id = "Populate" })