ورود

View Full Version : سوال: مشکل در ارتباط بین دو dropdownlist در mvc



mansoure_p
یک شنبه 26 فروردین 1397, 22:40 عصر
سلام من دو تا dropdown دارم که میخوام با هم ارتباط داشته باشن، با انتخاب dropdown اول که گروه مربوط به محصولات می باشد ، زیرگروه های مربوط به گروه انتخاب شده در dropdown دوم نمایش داده بشن.ولی انجام نمیشه من کدش رو هم نوشتم ولی نمیدونم مشکل کجاست.

این cshtml هست

<div class="form-group">
@Html.LabelFor(model => model.ProductSubGroup.ProductGroupID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("ProductGroupID", (SelectList)ViewBag.Type, "-- انتخاب ---", htmlAttributes: new { @class = "form-control",id = "rdbGroup" })
@Html.ValidationMessageFor(model => model.ProductSubGroup.ProductGroupID, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.ProductSubGroupID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("ProductSubGroupID", (SelectList)ViewBag.ProductSubGroupID, "-- انتخاب ---", htmlAttributes: new { @class = "form-control",id = "rdbSubGroup" })
@Html.ValidationMessageFor(model => model.ProductSubGroupID, "", new { @class = "text-danger" })
</div>
</div>



اینم کنترلر من هست


public ActionResult SelectCategory(int id)
{
var categoris = db.ProductSubGroup.Where(m => m.ProductGroup.ProductGroupID == id).Select(c => new { c.ProductSubGroupID, c.ProductSubGroupTitle});
return Json(categoris, JsonRequestBehavior.AllowGet);
}

// GET: Admin/Products/Create
public ActionResult Create()
{

ViewBag.ProductGroupID=new SelectList(db.ProductGroup,"ProductGroupID","ProductGroupTitle");
ViewBag.ProductSubGroupID = new SelectList(db.ProductSubGroup, "ProductSubGroupID", "ProductSubGroupTitle");


return View();
}


و اینم کد javascript هست


$('#rdbGroup').change(function () {
jQuery.getJSON('@Url.Action("SelectCategory")', { id: $(this).attr('value') }, function (data) {
$('#rdbSubGroup').empty();
jQuery.each(data, function (i) {
var option = $('<option></option>').attr("value", data[i].Id).text(data[i].Title);
$("#rdbSubGroup").append(option);
});
});
});

raha raad
یک شنبه 02 اردیبهشت 1397, 12:00 عصر
سلام. منم دقیقا همین اسکریپتو استفاده کردم و همین مشکل رو دارم....:متفکر:
مهندسین محترم کسی هست کمک کنه؟؟؟؟

Iran58
یک شنبه 02 اردیبهشت 1397, 14:51 عصر
سلام



@{
ViewBag.Title = "Index";
}
<script src="~/Scripts/jquery-1.10.2.js"></script>
<h2>Index</h2>


<div class="editor-label">
<br />
<div style="color: Purple;">
@Html.Label("استان را انتخاب کنید")
</div>
<div class="editor-field">
@if (ViewData.ContainsKey("State"))
{
@Html.DropDownList("State", ViewData["State"] as List<SelectListItem>, new { style = "width:250px", @class = "dropdown1" })
}
</div>
<br />
<div style="color: Purple;">
@Html.Label("شهر را انتخاب کنید", new { style = "width:250px" })
</div>
<div class="editor-field">
@Html.DropDownList("cities", new SelectList(string.Empty, "Value", "Text"), "شهر را انتخاب کنید", new { style = "width:250px", @class = "dropdown1" })
</div>
</div>






<script type="text/javascript">
$(document).ready(function () {
//Dropdownlist Selectedchange event
$("#State").change(function() {
$("#cities").empty();
$.ajax({
type: 'POST',
url: '/Home/GetCities', // we are calling json method
dataType: 'json',
data: { id: $("#State").val() },
success: function(cities) {
// states contains the JSON formatted list
// of states passed from the controller
$.each(cities, function(i, city) {
$("#cities").append('<option value="' + city.Value + '">' + city.Text + '</option>');
}); // here we are adding option for States
},
error: function(ex) {
alert('Failed to retrieve states.' + ex);
}
});
return false;
});
});
</script>




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace Cascadingdropdownlist_MVC.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
List<SelectListItem> li = new List<SelectListItem>();
li.Add(new SelectListItem { Text = "Select", Value = "0" });
li.Add(new SelectListItem { Text = "تهران", Value = "1" });
li.Add(new SelectListItem { Text = "خراسان", Value = "2" });
li.Add(new SelectListItem { Text = "کردستان", Value = "3" });
li.Add(new SelectListItem { Text = "ساری", Value = "4" });
ViewData["State"] = li;
return View();
}


public JsonResult GetCities(string id)
{
List<SelectListItem> cities = new List<SelectListItem>();
switch (id)
{
case "1":
cities.Add(new SelectListItem { Text = "انتخاب کنید", Value = "0" });
cities.Add(new SelectListItem { Text = "تهران", Value = "1" });
cities.Add(new SelectListItem { Text = "کرج", Value = "2" });
cities.Add(new SelectListItem { Text = "شهر ری", Value = "3" });
cities.Add(new SelectListItem { Text = "اسلامشهر", Value = "4" });
cities.Add(new SelectListItem { Text = "البرز", Value = "5" });
break;
case "2":
cities.Add(new SelectListItem { Text = "انتخاب کنید", Value = "0" });
cities.Add(new SelectListItem { Text = "خراسان شمالی", Value = "1" });
cities.Add(new SelectListItem { Text = "خراسان جنوبی", Value = "2" });
cities.Add(new SelectListItem { Text = "مشهد", Value = "3" });


break;
case "3":
cities.Add(new SelectListItem { Text = "انتخاب کنید", Value = "0" });
cities.Add(new SelectListItem { Text = "سنندج", Value = "1" });
break;
case "4":
cities.Add(new SelectListItem { Text = "انتخاب کنید", Value = "0" });
cities.Add(new SelectListItem { Text = "ساری", Value = "1" });
cities.Add(new SelectListItem { Text = "رشت", Value = "1" });
break;


}
return Json(new SelectList(cities, "Value", "Text"));
}
}
}