ASP MVC 4 IEnumerables

时间:2013-12-03 08:27:17

标签: asp.net-mvc asp.net-mvc-4 html-select

这是我的问题。我希望你们能帮助我。我是ASP MVC的新手,所以我会尝试非常具体。

我有一个汽车品牌的下拉列表,我想仅使用汽车模型填写第二个下拉列表,具体取决于我在第一个品牌上选择的品牌。

你能帮我吗?

控制器:

    public ActionResult Index()
    {
        Models.AgregarModel add = new Models.AgregarModel(); 

        List<SelectListItem> myBrand = new List<SelectListItem>(); 
        IEnumerable<SelectListItem> brands = add.Brands().Select(c => new SelectListItem 
        {
            Text = c.Brand,  
            Value = c.idBrand.ToString()
        });

        //I want to use the id of the selected brand in the next method Models which is waiting for 1 param (idBrand)

        List<SelectListItem> myModels = new List<SelectListItem>();
        IEnumerable<SelectListItem> elems = add.Models(//Here i don't know what param to give);                  

        List<SelectListItem> myYears = new List<SelectListItem>();
        IEnumerable<SelectListItem> anios = add.Seasons().Select(c => new SelectListItem
            {
                Text = c.Anio,
                Value = c.idAnio.ToString()
            });

        ViewBag.idBrand = marcas;
        ViewBag.idAnio = anios;

        return View();
    }

型号:

    public IEnumerable<MyBrands> Brands() 
    {       
        List<MyBrands> brands = (from b in db.MARCAs
                                 select new MyBrands { Brand = b.DESCRIPCION.ToString(), idBrand = b.ID_MARCA}).ToList();

        if (brands != null)
        {
            return brands;
        }
        else
            return null;
    }

    public IEnumerable<MyModels> Models(int idMarca)
    {
        var modelos = (from m in db.MODELOs
                       join b in db.MARCAs
                           on m.ID_MARCA equals b.ID_MARCA
                       where b.ID_MARCA == idMarca
                       select new MyModels
                       {
                           Modelo = m.DESCRIPCION.ToString(),
                           idMarca = m.ID_MARCA,
                           idModelo = m.ID_MODELO
                       }).ToList();

        return modelos;
    }

1 个答案:

答案 0 :(得分:1)

试试这个脚本:

<script type="text/javascript">
          $(document).ready(function () {
             $("#ddlBrands").change(function () {
              firstDDLValue = $("#ddlBrands").val();
               $.post('@Url.Action("LoadModels", "Home")', { fstValue: firstDDLValue },               function (result) {
                       var select = $("#ddlModels");
                       select.empty();
                       select.append($('<option/>', { value: '', text: '--Select--' }));
                       $.each(result, function (index, Data) {
                       select.append($('<option/>', {
                       value: Data.Value,
                       text: Data.Text
                       }));
                    });
               });
                    });
                  });
    </script>

在Controller上使用它:

public JsonResult LoadModels(string fstValue)
        {
            YourClassname obj= new YourClassname ();
            int Id = 0;
            if (fstValue != "")
                Id = Convert.ToInt32(fstValue);
            var result = obj.GetModelListByBrand(Convert.ToInt32(Id ));

            IList<SelectListItem> Data = new List<SelectListItem>();
            for (int i = 0; i < result.Count; i++)
            {
                Data.Add(new SelectListItem()
                {
                    Text = result[i].Text,
                    Value = result[i].Value,
                });
            }
            return Json(Data, JsonRequestBehavior.AllowGet);
        }