Ich versuche, Daten an den Controller für die weitere Verarbeitung übergeben, aber ich bekomme Null in Controller, aber Debug von js (Ajax) zeigt die Nummer trotzdem. Was könnte das Problem sein?Pass Daten von AJAX zu Controller in MVC
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('+');
});
Controller:
[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);
}
löschen 'content: "html",' und verwenden nur 'data: {id: id}' –
contentType: Datentyp, der an den Server gesendet wird, wo als Datentyp Datentyp der Daten von Server –