MVC Multiple DropDownLists在一个页面上?

时间:2013-05-07 15:20:18

标签: .net asp.net-mvc entity-framework asp.net-mvc-4

我是MVC的新手,我正在尝试在同一页面上绑定多个下拉列表,因此我可以从每个下载列表中获取所选值并使用它来搜索数据库。我的代码给了我一个无限循环。任何人都可以告诉我我做错了什么,或者推荐一种更好的方法来实现这个目标吗?

模型

public class SelectViewModel
    {

        public SelectList MakeList { get; set; }
        public SelectList ColorList { get; set; }
    }

控制器

  public ActionResult Index()
        {
            SelectViewModel objSelectModel = new SelectViewModel();
            List<Make> objMake = new List<Make>();
            List<Color> objColor = new List<Color>();

            objMake = GetMakeList();
            SelectList objBindMake = new SelectList(objMake, "MakeID", "Make1", 0);

            objColor = GetColorList();
            SelectList objBindColor = new SelectList(objColor, "ColorID", "Color1", 0);

            objSelectModel.MakeList = objBindMake;
            objSelectModel.ColorList = objBindColor;

            return View(objSelectModel);
        }



        public List<Make> GetMakeList()
        {
            var query = from m in makeRepository.Makes
                        select m;
            var makes = query.ToList();
            return makes;
        }

        public List<Color> GetColorList()
        {
            var query = from c in colorRepository.Colors
                        select c;
            var colors = query.ToList();
            return colors;
        }

查看

@model MvcApplication1.WebUI.Models.SelectViewModel
  Make : @Html.DropDownList("Make", new SelectList(Model.MakeList, "Value", "Text", Model.MakeList.SelectedValue))

  Color: @Html.DropDownList("Color", new SelectList(Model.ColorList, "Value", "Text", Model.ColorList.SelectedValue))

1 个答案:

答案 0 :(得分:0)

//Model
public class SelectViewModel
    {
        public string MakeId { get; set; }
        public string ColorId { get; set; }
        public IEnumerable<SelectListItem> MakeList { get; set; }
        public IEnumerable<SelectListItem> ColorList { get; set; }
    }

//Controller
        public PartialViewResult Index()
        {

            var query = makeRepository.Makes.Select(c => new SelectListItem
            {

                Value = SqlFunctions.StringConvert((double)c.MakeID),
                Text = c.Make1,
                Selected = c.MakeID.Equals(3)
            });

            var query1 = colorRepository.Colors.Select(c => new SelectListItem
            {

                Value = SqlFunctions.StringConvert((double)c.ColorID),
                Text = c.Color1,
                Selected = c.ColorID.Equals(3)
            });

            var model = new SelectViewModel
            {
                MakeList = query.AsEnumerable(),
                ColorList = query1.AsEnumerable()
            };
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Index(SelectViewModel model)
        {
            // here you can get the selected values
            return Content("Thanks for selecting make id: " + model.MakeId + "and color id:" + model.ColorId);
        }

//View
@model MvcApplication1.WebUI.Models.SelectViewModel
@using (Html.BeginForm())
{
@Html.DropDownListFor(m => m.MakeId, Model.MakeList, "--Select One--")
    @Html.DropDownListFor(m => m.ColorId, Model.ColorList, "--Select One--")
<input type="submit" value="Search" />
}