问题
The nested object values in the parameter does not follow. How can i get the full object including the nested values to follow in the actionmethod in the controller?
There is values in the object inside the red ring on the pictures when i call the ajac method. But does not follow in the controller
mapHub.client.requestForHelpInClient = function (requestDetails) {
debugger;
$.ajax({
type: "GET",
url: '@Url.Action("RequestPartialView", "Supplier")',
data: requestDetails,
success: function (response) {
$("#Request").html(response);
},
error: function (error) {
console.log(error);
}
}); }
function requestForHelp() {
requestDetails = {
CustomerId: @Model.Customer.CustomerID,
NumberOfHours: $("#numberOfHoursTextBox").val(),
TypeOfMachine: $("#typeOfMachineDropDownMenu").children("option:selected").val(),
CustomerLocation: CustomerPosition,
NearestSupplierList: nearestSuppliers
//StartDate: $( "#startDate" ).val(),
//EndDate: $( "#endDate" ).val(),
}
mapHub.server.requestForHelp(requestDetails);
public class RequestDetails
{
public int CustomerId { get; set; }
public Customer Customer { get; set; }
public MapClient CustomerLocation { get; set; }
public int NumberOfHours { get; set; }
public string TypeOfMachine { get; set; }
public List<MapClient> NearestSupplierList { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
The signalr hub
public void RequestForHelp(RequestDetails requestDetails)
{
requestDetails.Customer = Service.CustomerService.GetCustomerById(requestDetails.CustomerId);
Service.SupplierService.GetAspNetUserIDBySupplierID(requestDetails.NearestSupplierList[0].ClientId);
Clients.User(supplierAspNetUserID).requestForHelpInClient(requestDetails);
}
回答1:
Two quick changes for you.
You are using a
GETmethod.GETmethods cannot transfer data like that. Change theGETto aPOSTin both the controller and the ajax.You need to add a
[FromBody]tag in the controller. Depending on which version of MVC you are on the data binder can be extra picky about that.
Controller:
[HttpPost]
public ActionResult RequestPartialView([FromBody]RequestDetails reqDetails)
{
// code here
}
Ajax:
mapHub.client.requestForHelpInClient = function (requestDetails) {
$.ajax({
type: "POST",
url: '@Url.Action("RequestPartialView", "Supplier")',
data: requestDetails,
success: function (response) {
$("#Request").html(response);
},
error: function (error) {
console.log(error);
}
});
}
来源:https://stackoverflow.com/questions/58611646/nested-object-values-does-not-follow-in-parameter-from-ajax-call-to-actionmethod