我有一个控制器和一个Jquery。我想从JQuery中击中控制器。但我无法击中控制器。请告诉我出错的地方以及击中控制器所需的额外事项。
这行代码给了我错误$(this).load(raceId);
GET http://localhost:53987/Races/RacesName?id=103646584 500(内部 服务器错误)
控制器
public ActionResult RacesName(int race)
{
ClsRaces clsRaces = new ClsRaces();
race = clsRaces.RaceId;
return View();
}
脚本
var race = $(this).data("raceid");
var raceId = '@Url.Action("RacesName", "Races")?id=' + race;
$(this).load(raceId);
答案 0 :(得分:3)
您的方法有一个名为int race
的参数,但您没有为race
发送一个名称/值对(您发送的是一个名为id
的参数
将脚本更改为
var raceId = '@Url.Action("RacesName", "Races")?race=' + race;
或强>
将控制器方法更改为
public ActionResult RacesName(int id)
作为旁注,您可以使用浏览器工具(“网络”选项卡)来检查响应,该响应将包括正在抛出的异常的详细信息(在您的情况下,为{未提供的值] {1}}参数)
答案 1 :(得分:0)
实际上我们可以通过多种方式实现这一目标。
1)我建议的最好方法是编写ajax调用
$.ajax({
url: '/Races/RacesName',
dataType: 'text',
type: "GET",
async: true,
data: {
race: race
},
success: function (data) {
console.log('Success');
// Perform any action you would like to do.
// If you want to redirect to some other controller or method
// window.location.href= '/Races/Racedetails'
// If you want to perform UI actions
// $(.selector).hide(); $(.selector).show();
},
error: function (data) {
console.log('Error occured');
// Taking care of error // handle exceptions or errors
}
});
2)您可以直接使用window.location.href
window.location.href = '/Races/RacesName?race='+ race;
// This method can only redirect to respective controller action method. But further control will not be in our hands.