Url.Action提出了一个& 在我的网址中,我该如何解决这个问题?

Nie*_*ein 84 javascript c# jquery url.action

我想将变量itemId和entityModel发送到ActionResult CreateNote:

public ActionResult CreateNote(
        [ModelBinder(typeof(Models.JsonModelBinder))]
        NoteModel Model, string cmd, long? itemId, string modelEntity)
Run Code Online (Sandbox Code Playgroud)

用这个javascript:

Model.meta.PostAction = Url.Action("CreateNote", new { cmd = "Save", itemId = itemId, modelEntity = modelEntity});
Run Code Online (Sandbox Code Playgroud)

但是,发送的网址是

localhost:1304/Administration/blue/en-gb/Entity/CreateNote?modelEntity=Phrase&itemId=44     
Run Code Online (Sandbox Code Playgroud)

我想发送

localhost:1304/Administration/blue/en-gb/Entity/CreateNote?modelEntity=Phrase&itemId=44
Run Code Online (Sandbox Code Playgroud)

如何防止Url.Action将&发送到我要发送的第二个变量的前面?

K. *_*Bob 120

昨天我没有注意到你曾经&认为SO编辑改变了这一点.尝试包装你Url.Action()的a @Html.Raw()以防止&的编码.

或者只Url.Action()选择控制器/动作位并将两个参数作为后置数据传递而不是直接传递给url,jQuery应该以这种方式为你排序.

  • 这个解决方案有效,但为什么Url.Action会被编码?谈论意外行为. (39认同)

Kei*_*ith 13

我认为你的问题在于Model.meta.PostAction- 是属性string吗?

如果是这样,那么我的猜测就是你将它添加到页面中:

  • 剃刀: @Model.meta.PostAction
  • ASP视图引擎: <%:Model.meta.PostAction%>

两者都会自动为您编码该字符串.

要修复它,要么使用@Html.Raw()/ <%=(两者都不编码),要么使PostAction属性IHtmlString知道它已被编码:

string actionUrl = Url.Action("CreateNote", new { cmd = "Save", itemId = itemId, modelEntity = modelEntity});
Model.meta.PostAction = new HtmlString(actionUrl);
Run Code Online (Sandbox Code Playgroud)