我想通过AJAX传递列表。如何执行此操作并在运行时分配值。我正在做,但是它传递了空值。这是我的代码。
jQuery的:
for (var i = 0; i < 5; i++) { aabc += { id: i, color: 'Level' + i } + ","; } var str2 = aabc.replace(/,$/, " ") JSONString3 = [str2]; $.ajax({ url: '@Url.Action("test123", "ConfigurableTradeLane")', type: 'POST', contentType: 'application/json; charset=utf-8', data: JSON.stringify(JSONString3), success: function (data) { //alert("success"); }, error: function (jqXHR, exception) { alert('Error message.'); } });
控制器:
public void test123(List<TradelaneDetailViewModel> viewmodel) { //nothing }
它发送 “ item value is null” 。请帮我。
假设TradelaneDetailViewModel包含属性id和color,则脚本需要
TradelaneDetailViewModel
id
color
var items = []; for (var i = 0; i < 5; i++) { items.push({ id: i, color: 'Level' + i }); } $.ajax({ url: '@Url.Action("test123", "ConfigurableTradeLane")', type: 'POST', contentType: 'application/json; charset=utf-8', data: JSON.stringify({ viewmodel: items }), success: function (data) { //alert("success"); }, error: function (jqXHR, exception) { alert('Error message.'); } });