c# and jquery,
I have two drop-down lists(category and product), and have to change the product list based on the category.
@Html.DropDownList("categoryId", new SelectList(ViewBag.Category, "Id", "Name"), "Select Parent Category", new { @class = "form-control categorydata" })
@Html.DropDownList("productId", new SelectList(ViewBag.Product, "Id", "Name"), "Select Product", new { @class = "form-control product-list" })
And i am writing the following controller for getting the products list.It is worked.
[HttpGet]
public JsonResult GetProducts(int categoryId)
{
int tot = new Products().Total;
int cont = 1;
Category cat = new Category(2);
var products = new Products(cont, tot, cat);
// ViewBag.products = new Products(1,new Products().Total,new Category(categoryId));
return Json(products);
}
and my jquery is,
$('.categorydata').change(function () {
var selCat = $(".categorydata").val();
var url = "~/Add/GetProducts";
if (selCat.length != 0) {
$.get(url, { id: SelCat }, function (data) {
alert(data);
});
}
});
This jquery is didn't call the controller, and it can not get the data .
Please help me to solve it.
"~/Add/GetProducts"Use@Url.Action("ActionName", "Controller")and Also instead of$.getuse$.getJSONdataTypeto json.SelCatshould beselCat??