我是新来的MVC,并通过AJAX在我的ASP.NET应用程序的动作访问数据面临的一个问题。 这是我的Ajax代码:阿贾克斯不将数据返回到行动ASP.net MVC
$('.testBtn').click(function() {
$.ajax({
type: 'GET',
data: { id: 10 },
url="@Url.Action("GetData", "Consultation")",
success:function()
{
}
});
});
这里是我的控制器内的行动:
public ActionResult GetData(int id)
{
string x=id.ToStrring();
return null;
}
为了测试我只是路过一个静态的整数,让我的行动里它的价值。
单击按钮时我收到以下错误:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult GetData(Int32)' in 'careshade_mvc.Controllers.ConsultationController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
检查你的ajax类型是GET把它改为POST并尝试 –
@Div问题仍然存在.. –
还有一件事你必须检查,在你的mvc方法上面添加'[HttpPost]'。 –