ASP.NET MVC - 逗号分隔double作为控制器动作的参数

时间:2014-05-15 18:08:41

标签: asp.net-mvc parameters double comma controller-action

我有以下控制器操作:

public ActionResult AjaxQuantity(int productId = 0, double quantity = 0d, int periodId = 0)
{
   ...
}

和适当的ajax请求:

function Quantity(productId, quantity, periodId) {
    $.ajax({
        url: "@(Url.Action("AjaxQuantity"))",
        cache: false,
        type: "GET",
        data: { productId: productId, quantity: quantity, periodId: periodId },
        error: function () {
            Server503();
        }
    });
};

此外,请使用逗号作为小数分隔符。

当我使用例如" 12,34"进行ajax请求时作为数量,在控制器动作内我得到数量为1234d。

如果我将ajax请求的类型更改为" POST",那么我将所需的数量内部操作为12,34d。

GET请求发生了什么?看起来就像没有使用GET请求文化一样(逗号很简单)。

1 个答案:

答案 0 :(得分:1)

问题是 ','是保留的URI字符,因此您无法在GET参数中使用它。

POST参数作为请求正文发送,因为','也可以在那里使用。

来自Uniform Resource Identifiers (URI): Generic Syntax     2.2。保留字符

   Many URI include components consisting of or delimited by, certain
   special characters.  These characters are called "reserved", since
   their usage within the URI component is limited to their reserved
   purpose.  If the data for a URI component would conflict with the
   reserved purpose, then the conflicting data must be escaped before
   forming the URI.

      reserved    = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
                    "$" | ","