2

私は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))
4

1 に答える 1

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" />
}
于 2013-05-07T19:58:07.770 に答える