2015-09-06 61 views
1

传递参数jQuery中后一种方法在ASP.NET MVC我使用这个脚本调用控制器的方法:在控制器

<script> 
$.ajax({ 
    url: '@Url.Action("getBookedByUser", "Home")', 
    data: "2", 
    dataType: "html", 
    success: function (data) { 
     $('#someElement').html(data); // add the returned html to the DOM 

     console.log(data); 
    }, 
}); 
</script> 

,这就是我所说的方法:

public async Task getBookedByUser(string id) 
{ 
    BenchesService benchService = new BenchesService(); 
    List<Bench> obj = await benchService.getLastBenchByUser(id); 

    userBench = obj.ElementAt(0); 
} 

我的问题是我的控制器方法中的idnull。它不会像我打算的那样假设值“2”。

任何想法为什么?

回答

2

你快到了。你需要设置JSON以包含你的id属性:

$.ajax({ 
    url: '@Url.Action("getBookedByUser", "Home")', 
    data: { id: '2' }, 
    dataType: "html", 
    success: function (data) { 
     $('#someElement').html(data); // add the returned html to the DOM 

     console.log(data); 
    }, 
}); 
+1

哦,我明白了:)谢谢 – Ric