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¶m2=456。
I am attempting to do
我正在尝试去做
$("#myAjaxDiv").load(url);
But only param1
is getting populated in the action method.
但是只有param1在操作方法中被填充。
When I remove the &
and make it just &
then it works, but doing a string replace is super 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>
2 个解决方案
#1
14
By default every content (which is not IHtmlString
) emitted using a @
block is automatically HTML encoded by Razor (see this Razor intro article Html Encoding section)
默认情况下,使用@块发出的每个内容(不是IHtmlString)都是由Razor自动编码的HTML(请参阅Razor的入门文章HTML编码部分)
The Url.Action
returns just a plain string
so thats why the &
gets encoded.
Url。Action只返回一个纯字符串,因此&被编码。
Use the Html.Raw
if you don't want the encodeing:
使用Html。如果你不想要编入:
url = "@(Html.Raw(Url.Action("AjaxTest", new { year=2012, month=10})))";
#2
4
You can build the url in this way also.
您也可以以这种方式构建url。
var url = "@Url.Action("AjaxTest","YourControllerName")?year=2012&month=10";
$("#ajaxy").load(url);
#1
14
By default every content (which is not IHtmlString
) emitted using a @
block is automatically HTML encoded by Razor (see this Razor intro article Html Encoding section)
默认情况下,使用@块发出的每个内容(不是IHtmlString)都是由Razor自动编码的HTML(请参阅Razor的入门文章HTML编码部分)
The Url.Action
returns just a plain string
so thats why the &
gets encoded.
Url。Action只返回一个纯字符串,因此&被编码。
Use the Html.Raw
if you don't want the encodeing:
使用Html。如果你不想要编入:
url = "@(Html.Raw(Url.Action("AjaxTest", new { year=2012, month=10})))";
#2
4
You can build the url in this way also.
您也可以以这种方式构建url。
var url = "@Url.Action("AjaxTest","YourControllerName")?year=2012&month=10";
$("#ajaxy").load(url);