Retrive значения таблицы базы данных в выпадающем списке с помощью MVC 4
мой код просмотра
==============
@{ Layout = null; } <h2>Dealer</h2> <script src="~/Scripts/modernizr-2.5.3.js"></script> <script src="~/Scripts/jquery-2.1.1.min.js"></script> VARIANTNAME @Html.DropDownList("var1", "Choose VARIANTNAME") VEHICLENAME @Html.DropDownList("var2", "Choose VEHICLENAME") MODELNAME @Html.DropDownList("var3", "Choose MODELNAME") <script type="text/javascript"> $(function () { $("#var1").change(function () { var name = $("#var1 :selected").text(); //if user select the tournament var url = 'Home/Team'; var data1 = { "name": name }; $.post(url, data1, function (data) { //ajax call var items = []; items.push("<option value=" + 0 + ">" + "Choose VEHICLENAME" + "</option>"); //first item for (var i = 0; i < data.length; i++) { items.push("<option value=" + data[i].Value + ">" + data[i].Text + "</option>"); } //all data from the team table push into array $("#var2").html(items.join(' ')); }) //array object bind to dropdown list }); $("#var2").change(function () { //same logic for 3rd dropdown list var name = $("#var2 :selected").text(); var url = 'Home/Player'; var data1 = { "name": name }; $.post(url, data1, function (data) { var items = []; items.push("<option value=" + 0 + ">" + "Choose Player" + "</option>"); for (var i = 0; i < data.length; i++) { items.push("<option value=" + data[i].Value + ">" + data[i].Text + "</option>"); } $("#var3").html(items.join(' ')); }) }); }); </script> <input type="submit" value="submit" />
Код контроллера
======================
public class DealerController : Controller { // // GET: /Dealer/ string connection = ConfigurationManager.ConnectionStrings["DBconnectionString"].ConnectionString; List<SelectListItem> Vehicle = new List<SelectListItem>(); List<SelectListItem> MODELNAME = new List<SelectListItem>(); List<SelectListItem> Variant = new List<SelectListItem>(); public ActionResult Dealer() { ViewBag.var1 = GetOptions(); ViewBag.var2 = Vehicle; ViewBag.var3 = MODELNAME; return View(); } private SelectList GetOptions() { using (SqlConnection conn = new SqlConnection(connection)) { try { conn.Open(); SqlDataReader myReader = null; SqlCommand myCommand = new SqlCommand("SELECT VARIANTID, VARIANTNAME FROM VARIANT", conn); myReader = myCommand.ExecuteReader(); while (myReader.Read()) { Variant.Add(new SelectListItem { Text = myReader["VARIANTNAME"].ToString(), Value = myReader["VARIANTID"].ToString() }); } } catch (Exception e) { string msg = e.Message; } finally { conn.Close(); } return new SelectList(Variant, "Value", "Text", "id"); } } public JsonResult Team(string name) { using (SqlConnection conn = new SqlConnection(connection)) { conn.Open(); SqlDataReader myReader = null; SqlCommand myCommand = new SqlCommand("select VARIANTID,VEHICLENAME from VARIANT where VARIANTNAME ='" + name + "' ", conn); myReader = myCommand.ExecuteReader(); while (myReader.Read()) { Vehicle.Add(new SelectListItem { Text = myReader["VEHICLENAME"].ToString(), Value = myReader["VARIANTID"].ToString() }); } } return Json(Vehicle, JsonRequestBehavior.AllowGet); } public JsonResult Player(string name) { using (SqlConnection conn = new SqlConnection(connection)) { conn.Open(); SqlDataReader myReader = null; SqlCommand myCommand = new SqlCommand("select VARIANTID,MODELNAME from VARIANT where VEHICLENAME ='" + name + "' ", conn); myReader = myCommand.ExecuteReader(); while (myReader.Read()) { MODELNAME.Add(new SelectListItem { Text = myReader["MODELNAME"].ToString(), Value = myReader["VARIANTID"].ToString() }); } } return Json(MODELNAME, JsonRequestBehavior.AllowGet); }
Что я уже пробовал:
первое значение только работает и еще два скрипта или не работают.., пожалуйста, помогите кому-нибудь
Karthik_Mahalingam
получаете какую-либо ошибку в окне консоли?
Member 12753551
Никакой ошибки мы не получаем, но и не работаем
Karthik_Mahalingam
поместите отладчик и точку останова в код и проверьте, попадает ли он в метод