1

Via the 'data' properie on a ajax post i want to send a parameter to the action method. All values in the parameter object get through except the decimal/double values. Why is this and what can i do about it?

I have tried to change the value to string and even int. And it gets through but it is important that it gets through as a decimal or double.

                 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);
                    }
                });                
            }



[HttpPost]
    public ActionResult RequestPartialView(RequestDetails reqDetails)
    {
        RequestViewModel reqVm = new RequestViewModel() { requestDetails = reqDetails };
        return PartialView("RequestPartialView",reqVm);
    }

//This is the object i pass into requestForHelpInClient function that executes the ajax call

 public class RequestDetails
{
    public int CustomerId { get; set; }
    public Customer Customer { get; set; }
    public int NumberOfHours { get; set; }
    public string TypeOfMachine { get; set; }
    public List<Supplier> NearestSupplierList { get; set; }
}
public class Customer : MapClient
{
    public int CustomerID { get; set; }
    public string AspNetUserID { get; set; }
    public string Name { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

every value gets through from the ajax call to the action method parameter excepts the latitude and longitude decimal values.

I get no error message. The value just says '0'

  • Possible duplicate of [C# MVC Controller cannot get decimal or double values from Ajax POST request](https://stackoverflow.com/questions/32908503/c-sharp-mvc-controller-cannot-get-decimal-or-double-values-from-ajax-post-reques) – Aage Nov 04 '19 at 16:03
  • I have now tried to do the solution with the modelbinding in the linked post without succes. @aage –  Nov 04 '19 at 16:20
  • My question is a bit different because i send a whole .net object into the parameter. In the example it is just one value which yo can easily convert to string to then convert back to decimal/double. My value is nested inside my requestdetails object –  Nov 04 '19 at 16:26

1 Answers1

1

You need to stringify your object before posting mapHub.client.requestForHelpInClient = function (requestDetails) { $.ajax({ type: "POST", url: '@Url.Action("RequestPartialView", "Supplier")', data: JSON.stringify(requestDetails), success: function (response) { $("#Request").html(response); }, error: function (error) { console.log(error); } });
}

That should fix it

code-assassin
  • 415
  • 4
  • 14
  • i now get null in my action method parameter. I have changed the datatype of the parameter to string. Is it correct? @code-assassin –  Nov 04 '19 at 19:45
  • the datatype should not be string. It should be RequestDetails since that is what you are stringifying – code-assassin Nov 08 '19 at 08:54
  • Does not work. Now i get a requestDetails but all of the values in the parameter object are 0 or null. Have i missed anything? @code-assassin –  Nov 12 '19 at 11:09
  • In your ajax post can u change the url to url: "/Supplier/RequestPartialView" and test. Log the data too before Posting just to make sure its good – code-assassin Nov 20 '19 at 12:32