1

我有一个控制器和一个 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);
4

2 回答 2

3

您的方法有一个名为的参数int race,但您没有发送一个名称/值对race(您发送的是一个名为 的参数id

将脚本更改为

var raceId = '@Url.Action("RacesName", "Races")?race=' + race;

或者

将控制器方法更改为

public ActionResult RacesName(int id)

作为旁注,您可以使用浏览器工具(“网络”选项卡)来检查响应,其中将包括正在抛出的异常的详细信息(在您的情况下,没有为race参数提供值)

于 2018-04-30T01:46:46.527 回答
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.
于 2018-04-30T04:29:58.003 回答