Nested object values does not follow in parameter from ajax call to actionmethod

狂风中的少年 提交于 2020-01-07 08:23:15

问题


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.

  1. You are using a GET method. GET methods cannot transfer data like that. Change the GET to a POST in both the controller and the ajax.

  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!