我想從視圖傳遞一個對象到控制器使用Ajax。但我在控制器中的對象內獲得空值。我的代碼如下..你能幫我解決這個問題Ajax調用返回空值的對象
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
function Save() {
// var Trip={output};
debugger;
var stop;
var table = $('#scheduletable').DataTable();
var rows = $('#scheduletable tr');
var StopNames = '';
var TripNumbers = '';
var TripDetails = '';
StopNames += table.column(0).data().toArray().join("&");
for (var i = 1; i < rows[0].cells.length; i++) {
TripNumbers += rows[0].cells[i].innerText + '&';
TripDetails += table.column(i).data().toArray().join("&");
}
debugger;
TripNumbers = TripNumbers.replace(/(\r\n|\n|\r)/gm, "");
var output = {
'StopNames': StopNames,
'TripNumbers': TripNumbers,
'TripDetails': TripDetails
};
$.ajax({
type: "POST",
url: "/ScheduleManagement/ScheduleManagement/SaveSchedules",
// contentType: 'application/json;',
dataType: "json",
data: JSON.stringify(output),
success: function (values) {
},
error: function (err) {
console.log(err);
}
});
}
我使用控制器下面的代碼
[HttpPost]
public JsonResult SaveSchedules(TripsToSave output)
{
// dynamic item = output;
int serviceId = Convert.ToInt32(Session["ServiceGroupId"]);
// int status= _scheduleManagementBL.SaveMasterSchedule(output);
return Json("1", JsonRequestBehavior.AllowGet);
// return RedirectToAction("DisplayHttpError", "Error");
}
實體。我正在使用此代碼
public class TripsToSave
{
public string StopNames { get; set; }
public string TripNumbers { get; set; }
public string TripDetails { get; set; }
}
你通過你直接StopNames,TripNumbers得到PARAMS Ajax請求數據的方式, TripDetails不通過輸出。 –
只是'數據:輸出,'(或者如果你真的把它串起來,那麼你需要'contentType:'application/json;'') –