C# MVC控制器方法参数始终为空
我的javascript函数正在调用我的MVC4控制器,但参数始终为null。这似乎是一个常见的问题,我也尝试了一些我研究过的东西,但都不管用。知道为什么它总是空的吗 My javascript GetEntries()函数可正确创建显示以下值的警报:C# MVC控制器方法参数始终为空,c#,javascript,asp.net-mvc-4,C#,Javascript,Asp.net Mvc 4,我的javascript函数正在调用我的MVC4控制器,但参数始终为null。这似乎是一个常见的问题,我也尝试了一些我研究过的东西,但都不管用。知道为什么它总是空的吗 My javascript GetEntries()函数可正确创建显示以下值的警报: function GetEntries(firstLetter) { alert(firstLetter); $.post('/Home/GetEntries', firstLetter,
function GetEntries(firstLetter) {
alert(firstLetter);
$.post('/Home/GetEntries',
firstLetter,
EntriesReceived());
}
我的控制器方法断点被命中:
public void GetEntries(string firstLetter)
{
Debug.WriteLine(firstLetter);
}
但是,firstLetter始终为空。我不知道该怎么办
尝试失败:
[HttpPost]
public void GetEntries(string firstLetter)
{
Debug.WriteLine(firstLetter);
}
[HttpPost]
public void GetEntries(string id)
{
Debug.WriteLine(id);
}
我尝试使用JSON.stringify发布。
function GetEntries(firstLetter) {
alert(firstLetter);
var firstLetterAsJson = JSON.stringify(firstLetter);
$.post('/Home/GetEntries',
{ jsonData: firstLetterAsJson },
EntriesReceived());
}
我尝试将HttpPost属性添加到我的控制器:
[HttpPost]
public void GetEntries(string firstLetter)
{
Debug.WriteLine(firstLetter);
}
[HttpPost]
public void GetEntries(string id)
{
Debug.WriteLine(id);
}
我尝试将参数名称更改为“id”以匹配我的路由映射:
[HttpPost]
public void GetEntries(string firstLetter)
{
Debug.WriteLine(firstLetter);
}
[HttpPost]
public void GetEntries(string id)
{
Debug.WriteLine(id);
}
以下几点应该有效
function GetEntries(firstLetter) {
$.post('/Home/GetEntries', { firstLetter: firstLetter }, EntriesReceived);
}
还要注意,EntriesReceived
回调如何作为第三个参数传递给$.post
函数。在代码中,您似乎在调用函数(EntriesReceived()
),而不是将其作为回调传递。在此,我假设此函数的定义如下:
function EntriesReceived(result) {
// handle the result of the AJAX call here
}
如果要将其作为JSON请求发送,则应使用$.ajax方法,该方法允许您指定正确的请求内容类型:
function GetEntries(firstLetter) {
$.ajax({
url: '/Home/GetEntries',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ firstLetter: firstLetter }),
success: function(result) {
// handle the result of the AJAX call here
}
});
}
我看到的控制器操作的另一个问题是,您将其定义为void
。在ASP.NET MVC中,常见的惯例是所有控制器操作必须返回ActionResult
类的实例。但是,如果您不想向客户端返回任何内容,请使用此案例的特定ActionResult-EmptyResult:
[HttpPost]
public ActionResult GetEntries(string firstLetter)
{
Debug.WriteLine(firstLetter);
return new EmptyResult();
}
以下几点应该有效
function GetEntries(firstLetter) {
$.post('/Home/GetEntries', { firstLetter: firstLetter }, EntriesReceived);
}
还要注意,EntriesReceived
回调如何作为第三个参数传递给$.post
函数。在代码中,您似乎在调用函数(EntriesReceived()
),而不是将其作为回调传递。在此,我假设此函数的定义如下:
function EntriesReceived(result) {
// handle the result of the AJAX call here
}
如果要将其作为JSON请求发送,则应使用$.ajax方法,该方法允许您指定正确的请求内容类型:
function GetEntries(firstLetter) {
$.ajax({
url: '/Home/GetEntries',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ firstLetter: firstLetter }),
success: function(result) {
// handle the result of the AJAX call here
}
});
}
我看到的控制器操作的另一个问题是,您将其定义为void
。在ASP.NET MVC中,常见的惯例是所有控制器操作必须返回ActionResult
类的实例。但是,如果您不想向客户端返回任何内容,请使用此案例的特定ActionResult-EmptyResult:
[HttpPost]
public ActionResult GetEntries(string firstLetter)
{
Debug.WriteLine(firstLetter);
return new EmptyResult();
}
成功了。我真的很感谢你的快速帮助。在我对这个问题的研究中,我没有看到它说在哪里做
{firstLetter:firstLetter}
。也许我只是错过了。非常感谢。是的,有一个EntriesReceived()函数,如您所述。我也同意不返回空。我只是还没走那么远。很好的提示。非常感谢你详尽准确的回答。在过去的几个月里,你对我的帮助比你所知道的要多。再次感谢你,成功了。我真的很感谢你的快速帮助。在我对这个问题的研究中,我没有看到它说在哪里做{firstLetter:firstLetter}
。也许我只是错过了。非常感谢。是的,有一个EntriesReceived()函数,如您所述。我也同意不返回空。我只是还没走那么远。很好的提示。非常感谢你详尽准确的回答。在过去的几个月里,你对我的帮助比你所知道的要多。再次感谢。