javascript - 如何正确创建带有 'Url.Action' 查询字符串的 url,以便它在 Firefox 中工作

标签 javascript c# asp.net-mvc razor model-binding

在 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.0a2Opera 35.0 中,请求的 URL 如下所示:

http://localhost/App/Controller/Action?a=True&amp;b=123&amp;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/

相关文章:

javascript 在空格时分割但并非总是如此

javascript - 如何进行不区分大小写的文本输入?

c# - 从字符串构建表达式<Func<T,bool>>

jquery - 从我的服务器发送的静态内容(例如 : . css、.js)是否被 gzip 压缩?

asp.net-mvc - ASP.net MasterPageFile 属性是什么?

c# - 在 ASP.NET MVC 单元测试中使用 Moq 模拟函数时无法返回非空字符串

javascript - 我可以设置 TEXTAREA 元素的高度但让用户稍后调整它的大小吗?

javascript - 使用 .prependTo() 时奇怪的边距

C# 动态创建的 LinkBut​​ton 命令事件处理程序

C# 缩放 GDI 位置但不缩放字体大小或线条粗细