编程语言
首页 > 编程语言> > javascript-小数时Ajax方法未命中控制器

javascript-小数时Ajax方法未命中控制器

作者:互联网

我正在尝试在Web应用程序中添加类似时间注册的功能.用户具有一个表单,可以在其中选择他从事的项目,然后输入在该项目上花费的时间.

现在可以正常工作,直到您开始增加半小时.像5.5.
由于某种原因,ajax不会触发控制器.当我console.log响应时,它说参数“ hours”不能为空,应将其设置为可选.因此,我推测由于某种原因,如果它是小数,则不会占用我的变量.

function AddTimeReg() {
...
 var hours;
    var language = window.navigator.userLanguage || window.navigator.language;
    if (language === "en-US"){
         hours = $("#Hours").val().trim().replace(",", ".");
    }
    else{
         hours = $("#Hours").val().trim().replace(".", ",");
    }
   ...
        $.ajax({
            type: 'post',
            url: appPath + '/TimeReg/ShopDoc',
            data: {
                regDate: date, salesOrder: salesorder, shopDoc: shopdoc, startTime: convertedStartTime, endTime: convertedEndTime,
                hours: hours, info: info, timeRegLineNr: timeRegLineNr
            },
...
}

和控制器:

public ActionResult ShopDoc(DateTime regDate, string salesOrder, string shopDoc, string startTime, string endTime, decimal hours, string info, int timeRegLineNr)
  {
      ... 
  }

因此,由于某种原因:它可以在5、2或8等小时内正常工作.但是如果输入5.5或2.5,则控制器不会被断点击中.

编辑:不是链接的重复项.在链接中,我们讨论的是将URL中的参数作为actionresult给出,而问题在c#中出现,而在这里,我相信JS中缺少某些内容.

解决方法:

确保您要发送的十进制类型以…开头(如您的代码所示),它当前正在发送字符串而不是十进制…

在hours属性上使用parseFloat(),以便在将其发送到控制器时将其正确序列化为JSON.

编辑:

这是一个具有约束力的问题…

我们需要将控制器上的参数提取到模型中

public class ShopDocModel
{
    public DateTime regDate   {get;set;} 
    public string salesOrder  {get;set;}
    public string shopDoc     {get;set;}
    public string startTime   {get;set;}
    public string endTime     {get;set;}
    public decimal hours      {get;set;}
    public string info        {get;set;}
    public int timeRegLineNr { get; set; }
}

然后在控制器参数中引用它

public ActionResult ShopDoc(ShopDocModel model)

那应该解决你的问题

编辑

$.ajax({
        type: 'post',
        url: appPath + '/TimeReg/ShopDoc',
        data: JSON.stringify({
            regDate: date, salesOrder: salesorder, shopDoc: shopdoc, startTime: convertedStartTime, endTime: convertedEndTime,
            hours: hours, info: info, timeRegLineNr: timeRegLineNr
        }),
contentType: 'application/json; charset=utf-8',

标签:ajax,asp-net,javascript,c,asp-net-mvc
来源: https://codeday.me/bug/20191210/2104981.html