我试图使用JQuery Ajax GET方法调用MVC控制器操作,并传递这两个参数。 问题是操作没有被触发而是给出404错误。下面是我的代码,请指出我缺少的内容。
控制器中的动作
[HttpGet]
public JsonResult GetViewGridData(List<Models.ItemModel> gridData, int Category_Id = 0, int SubCategory_Id = 0)
{
//code goes here
}
Ajax调用
var Category_Id = $("#CategoryView").val();
var SubCategory_Id = $("#SubCategoryView").val();
$.ajax({
url: "/Item/GetViewGridData/",
data: {Category_Id:Category_Id,SubCategory_Id:SubCategory_Id},
type: "GET",
dataType: "JSON",
success: function (data) {
//});
},
error: function (data) {
}
});
答案 0 :(得分:0)
试试这个
function GetViewGridData() {
var Category_Id = $("#CategoryView").val();
var SubCategory_Id = $("#SubCategoryView").val();
jQuery.ajax({
url: '/GetViewGridData/Item?SubCategory_Id='+ SubCategory_Id +'& Category_Id='+ Category_Id,
method: "GET",
cache: false
}).done(function (result) {
//You will get the data in result
});
}
答案 1 :(得分:0)
转到属性路由 - 在路由配置中启用它
routes.MapMvcAttributeRoutes()
并在您的Item
控制器中
[RoutePrefix("item")]
public class ItemController: Controller
{
[HttpGet]
[Route("GetViewGridData")]
public JsonResult GetViewGridData(List<Models.ItemModel> gridData, int Category_Id = 0, int SubCategory_Id = 0)
{
//code goes here
}
}
正如@Stephen评论的那样,如果你不使用它,也要从动作中删除不需要的参数