【问题标题】:Actionmethod cant get double value in parameter from ajax postAction 方法可以从 ajax post 获取参数中的双值
【发布时间】:2019-11-04 15:50:57
【问题描述】:

通过 ajax 帖子上的“数据”属性,我想向操作方法发送一个参数。参数对象中的所有值都通过,除了十进制/双精度值。为什么会这样?我该怎么办?

我尝试将值更改为字符串甚至 int。它可以通过,但重要的是它以小数或双精度形式通过。

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

//这是我传递给requestForHelpInClient函数执行ajax调用的对象

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

除了纬度和经度十进制值之外,每个值都从 ajax 调用到 action 方法参数。

我没有收到任何错误消息。值只是说'0'

【问题讨论】:

  • 我现在尝试使用链接帖子中的模型绑定来解决问题,但没有成功。 @aage
  • 我的问题有点不同,因为我将整个 .net 对象发送到参数中。在示例中,它只是一个值,您可以轻松地将其转换为字符串,然后再转换回十进制/双精度值。我的值嵌套在我的 requestdetails 对象中

标签: javascript c# ajax asp.net-mvc


【解决方案1】:

您需要在发布之前对您的对象进行字符串化 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); } });
}

应该可以解决的

【讨论】:

  • 我现在在我的操作方法参数中得到空值。我已将参数的数据类型更改为字符串。这是正确的吗? @代码刺客
  • 数据类型不应该是字符串。它应该是 RequestDetails ,因为这就是您要字符串化的内容
  • 不起作用。现在我得到一个 requestDetails 但参数对象中的所有值都是 0 或 null。我错过了什么吗? @代码刺客
  • 在您的 ajax 帖子中,您可以将 url 更改为 url:"/Supplier/RequestPartialView" 并进行测试。在发布之前也要记录数据,以确保其良好
猜你喜欢
  • 2019-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多