在 razor View script
中,会像这样创建一个带有查询字符串的 url,当单击某个元素时,window.location
将被设置为带有查询字符串的该 url。
<script type="text/javascript">
// ...
var url = "@Url.Action("action", "controller",
new
{
a = model.a,
b = model.b,
c = model.c
}, null)";
$("#something").on("click", function () {
window.location = url;
});
// ...
</script>
这在 Internet Explorer 11 中工作正常,请求的 URL 如下所示:
http://localhost/App/Controller/Action?a=True&b=123&c=False
但在 Firefox 47.0a2 和 Opera 35.0 中,请求的 URL 如下所示:
http://localhost/App/Controller/Action?a=True&b=123&c=False
请注意查询字符串中每个 &
后面的 amp;
。此amp;
会导致 Firefox 和 Opera 的模型绑定(bind)失败(在 IE 11 中模型绑定(bind)没有错误)。
为什么 Firefox 和 Opera 的行为与 IE 不同?
<小时/>错误如下所示:
The parameters dictionary contains a null entry for parameter 'a' of non-nullable type 'type' for method 'method' in 'controller'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parametername: parameters
最佳答案
MVC将对@Url.Action
进行编码。您可以简单地告诉它不要使用 @Html.Raw(Url.Action()) 进行编码
关于javascript - 如何正确创建带有 'Url.Action' 查询字符串的 url,以便它在 Firefox 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36068404/