【发布时间】:2017-09-30 17:53:59
【问题描述】:
我正在尝试将数据传递给控制器以进行进一步处理,但我在控制器中得到 null,但是 js (ajax) 的调试无论如何都会显示该数字。可能是什么问题?
阿贾克斯:
$('.toggler_btn').on('click', function (event)
{
var id = $(this).attr('data-id');
if ($(this).text() === '+') {
$.ajax({
url: "/List/GetSubItems",
type: "POST",
contentType: "html",
dataType: "text",
data: '{"id":"' + id + '"}', // here it show the data, like 5 or some other number
success: function (data)
{
$('.element_wrapper [data-id="' + id + '"]').html(data);
}
})
$(this).html('-');
}
else $(this).html('+');
});
控制器:
[HttpPost]
public ActionResult GetSubItems(string id) // here I get null
{
int pID = 0;
int.TryParse(id, out pID);
List<H_Table> list = new List<H_Table>();
foreach (var item in db_connection.H_Table.Where(i => i.PARENT_ID == pID))
{
list.Add(item);
}
return PartialView(list);
}
【问题讨论】:
-
删除
contentType: "html",,只使用data: { id: id } -
contentType : 要发送到服务器的数据类型,其中 dataType 从服务器返回数据的数据类型
标签: javascript jquery ajax asp.net-mvc