ajax 使用 MVC3 @Url.Action 的 JQuery 加载没有正确传递参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12866144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 10:51:24  来源:igfitidea点击:

JQuery Load using MVC3 @Url.Action does not pass parameters properly

ajaxasp.net-mvc-3

提问by Nathan Koop

I noticed that doing @Url.Action("myAction", new { param1 = 123, param2 = 456})provides me with an invalid URL Home/myAction?param1=123&param2=456.

我注意到这样做为@Url.Action("myAction", new { param1 = 123, param2 = 456})我提供了一个无效的 URL Home/myAction?param1=123&param2=456

I am attempting to do

我正在尝试做

$("#myAjaxDiv").load(url);

But only param1is getting populated in the action method.

但只有param1在 action 方法中填充。

When I remove the &and make it just &then it works, but doing a string replace is super hacky.

当我删除&并使其&工作时,它就可以工作,但是进行字符串替换是超级hacky。

url = url.replace("&", "&");

Am I missing something here?

我在这里错过了什么吗?

EDIT:Per request I'm including some of my sample app. (you can create a new MVC app and just add these quickly and see for yourself)

编辑:根据请求,我包含了一些示例应用程序。(您可以创建一个新的 MVC 应用程序,然后快速添加这些并亲自查看)

Controller:

控制器:

public ActionResult AjaxTest(int? year, int? month)
{
    ViewBag.Message = string.Format("Year: {0}, Month: {1}", year.HasValue ? year.ToString() : "no year", month.HasValue ? month.ToString() : "no month");
    return PartialView("AjaxTest");
}

AjaxTest View:

AjaxTest 视图:

@ViewBag.Message

Index View:

索引视图:

<script>
    $(function () {
        var url="";
        $("#noParams").click(function () {
            url = "Home/AjaxTest";
            $("#ajaxy").load(url)
            $("#url").text(url);
        });
        $("#yearParam").click(function () {
            url = "Home/AjaxTest?year=2012";
            $("#ajaxy").load(url)
            $("#url").text(url);
        });
        $("#yearAndMonthParam").click(function () {
            url = "Home/AjaxTest?year=2012&month=10";
            $("#ajaxy").load(url)
            $("#url").text(url);
        });

        $("#generated").click(function () {
            url = "@(Url.Action("AjaxTest", new { year=2012, month=10}))";
            $("#ajaxy").load(url);
            $("#url").text(url);
        });

    });
</script>

<a id="noParams" href="#">No Params</a> <br />
<a id="yearParam" href="#">Year Param</a> <br />
<a id="yearAndMonthParam" href="#">Year and Month Param</a> <br />
<a id="generated" href="#">Generated</a> <br />
<div id="ajaxy">

</div>

<div>
URL: <span  id="url"></span>
</div>

回答by nemesv

By default every content (which is not IHtmlString) emitted using a @block is automatically HTML encoded by Razor (see this Razor intro articleHtml Encoding section)

默认情况下,IHtmlString使用@块发出的每个内容(不是)都由 Razor 自动进行 HTML 编码(请参阅此Razor 介绍文章Html 编码部分)

The Url.Actionreturns just a plain stringso thats why the &gets encoded.

Url.Action回报率只是一个普通的string所以这就是为什么在&被编码。

Use the Html.Rawif you don't want the encodeing:

使用Html.Raw,如果你不希望encodeing:

url = "@(Html.Raw(Url.Action("AjaxTest", new { year=2012, month=10})))";

回答by Shyju

You can build the url in this way also.

您也可以通过这种方式构建 url。

var url = "@Url.Action("AjaxTest","YourControllerName")?year=2012&month=10";
$("#ajaxy").load(url);