Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc ASP MVC 4 IEnumerables_Asp.net Mvc_Asp.net Mvc 4_Html Select - Fatal编程技术网

Asp.net mvc ASP MVC 4 IEnumerables

Asp.net mvc ASP MVC 4 IEnumerables,asp.net-mvc,asp.net-mvc-4,html-select,Asp.net Mvc,Asp.net Mvc 4,Html Select,这是我的问题。我希望你们能帮助我。我对ASP MVC非常陌生,所以我会尽量说得非常具体 我有一个关于汽车品牌的下拉列表,我想在第二个下拉列表中只填充汽车型号,这取决于我在第一个列表中选择的品牌 你能帮我吗 控制器: public ActionResult Index() { Models.AgregarModel add = new Models.AgregarModel(); List<SelectListItem> myBran

这是我的问题。我希望你们能帮助我。我对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 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);
        }
型号:

    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;
    }
请尝试以下脚本:

<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>
在控制器上使用此选项:

    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 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);
        }
请尝试以下脚本:

<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>
在控制器上使用此选项:

    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 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);
        }

您可以尝试jquery在下拉列表中绑定数据..您可以尝试jquery在下拉列表中绑定数据..非常感谢!我是一个网络开发方面的新手,需要一段时间才能完成。。但这正是我要找的!谢谢一切都很好!非常感谢你!我是一个网络开发方面的新手,需要一段时间才能完成。。但这正是我要找的!谢谢一切都很好!