0

我知道这很简单,但对 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>
4

2 回答 2

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()) 
于 2013-09-24T20:17:23.547 回答
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" })
于 2013-09-24T20:07:59.807 回答