Hi Dear,
i have to call action controller method on dropdownlist onchange event and load value to another dropdownlist so i used ajax but not working. my code below
Controller method
[HttpPost]
public JsonResult WriteCoordiatesNew(int catid)
{
try
{
List<SubCategory> sublstcontent = new List<SubCategory>();
{
sublstcontent = DatabaseAccess.SubCategory(catid);
};
List<SelectListItem> subcatlist = new List<SelectListItem>();
foreach (var item in sublstcontent)
{
subcatlist.Add(new SelectListItem
{
Text = item.sub_cate,
Value = item.sub_cat_id.ToString()
});
}
return Json(subcatlist, JsonRequestBehavior.AllowGet);
}
catch (Exception ee)
{
Console.WriteLine(ee.Message);
}
return null;
}
In View
@Html.Label("Select Category")
@Html.DropDownList("category_name", (IEnumerable<SelectListItem>)ViewBag.categorylist, "--Select--", new { id = "drpCate", onChange = "dropdownsub()" })
<br />
@Html.Label("Select Sub-Category")
@Html.DropDownList("subcat_name", (IEnumerable<SelectListItem>)ViewBag.SubCatLst, "--Select--", new { id = "drpSubCate" })
Ajax or javascript
function dropdownsub() {
var urlMap = document.getElementById('SubCat').value;
var sel = document.getElementById('drpCate').value;
alert(sel);
try {
$.ajax({
url: urlMap,
type: "POST",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ 'catid': sel }),
success: function (mydata) {
alert('Success');
}
});
}
catch (e) {
alert(e);
}
}