【问题标题】:date parameters to mvc action from jquery ajax are always null从 jquery ajax 到 mvc 操作的日期参数始终为空
【发布时间】:2013-12-17 09:59:40
【问题描述】:

我有以下来自 jquery 的 ajax 调用代码。

  $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: oAxn.ds,
            data: JSON.stringify(oAxn.p(graphsDrpDwn)),
            beforeSend: function () {
                $(".errorMsg").hide();
                YC.toggleLoadingImg(tabId, false);
            },
            success: function (graphData) {
                if (typeof oAxn.s === "function") {
                    if (graphData.reportData.ErrorMsg !== "Success") {
                        $(".errorMsg").show().html(graphData.reportData.ErrorMsg);
                        $("#tabs").hide();
                        YC.toggleLoadingImg(tabId, true);
                    } else {
                        //Inorder to avoid flicker, we hide in style sheet on page load.
                        $("#tabs").show();
                        $(".lblDrp").show();
                        $("#MainDiv").show();
                        YC.showRangeSel();
                        YC.toggleLoadingImg(tabId, true);
                        oAxn.s(graphData, grphOneID, grphTwoID);
                    }
                }
            },
            error: function (err) {
                YC.toggleLoadingImg(tabId, true);
            }
        });

获取参数JSON.stringify(oAxn.p(graphsDrpDwn))的方法是:

getDealerParams: function (graphsDrpDwn) {
        /// <summary>We gather the parameters required for dealer tab</summary>
        /// <param name="graphsDrpDwn" type="string">Holds the dropdown list id</param>

        return {
            //We get these from hidden inputs, since they won't be available
            //  in the query string for framed in reports
            shopId: $(".txtShopId").val(),
            siteId: $("." + graphsDrpDwn).find(":selected").attr("data-field"),
            dealerId: $("." + graphsDrpDwn).find(":selected").val(),
            frmDate: $(".tsFrom").val(),
            toDate: $(".tsTo").val()
        };
    },

除frmDate、toDate 外,所有参数都在获取值。 我可以看到客户端的值,但是当传递给控制器​​时,两个总是空的。 它只发生在我的系统中,在我同事的机器上运行良好。 有人可以建议这里可能出了什么问题吗?

【问题讨论】:

  • 由于问题出在日期字段且仅在某些机器上,可能是日期字段和/或区域性的默认设置之间存在差异,导致无法正确解析日期.尝试使用月份和日期都小于 13 的日期,看看它们是否有效。
  • 是的...当两者​​都小于 13 时,它确实有效。我在哪里可以更改设置以允许 mm dd?

标签: asp.net-mvc-3 jquery


【解决方案1】:

这不是理想的解决方案,但它适用于我们的几个项目。我们在 Global.asax 中加入以下代码:

protected void Application_BeginRequest(Object sender, EventArgs e)
    {
        var newCulture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
        newCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
        newCulture.DateTimeFormat.DateSeparator = "/";
        Thread.CurrentThread.CurrentCulture = newCulture;
    }

当然,您需要将日期格式更改为您需要的格式。

【讨论】:

    【解决方案2】:

    在 aspx 文件中为页面指令添加以下属性对我有用。

    UICulture="es" Culture="es-US"

    【讨论】:

      猜你喜欢
      • 2011-04-04
      • 2020-12-27
      • 1970-01-01
      • 2015-05-29
      • 2020-05-22
      • 2012-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多