我在控制器中有这两个方法,我想从js调用GetCountryId并将Id传递给它,但由于路由它总是点击Index方法,如何解决这个问题?
[Route("/AutoComplete/{id}")]
public IActionResult Index(string id, string text, int limit,int? CountryId)
{
//Some Code
return View();
}
public IActionResult GetCountryID(string CountryID)
{
//Some Code
return View();
}
和JS代码是
$.ajax({
type: 'get'
url: '/AutoComplete/GetCountryID',
data: { CountryID: 100},
success: function (result) {
alert("Success!");
}});
感谢。
答案 0 :(得分:0)
我通过将行改为
解决了这个问题 url: '/AutoComplete/GetCountryID/',
答案 1 :(得分:0)
@StephenMuecke的建议非常好。它的常见问题和路由属性可以帮助您解决类似的问题。在你的情况下,我看到几个问题。
GetCountryID
操作中接收字符串 - 您应该使用int而不是string。 [HttpGet("/AutoComplete/{CountryID}")]
我在这里提供了非常快速的演示: 为什么你应该使用这个解决方案而不是你的?