97

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

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

使用这个 javascript:

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

但是,正在发送的 url 是

localhost:1304/Administration/blue/en-gb/Entity/CreateNote?modelEntity=Phrase&itemId=44     

我要发送

localhost:1304/Administration/blue/en-gb/Entity/CreateNote?modelEntity=Phrase&itemId=44

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

4

2 回答 2

137

昨天我没有注意到你有&我认为是 SO 编辑器改变了它。尝试将您包装Url.Action()在 a@Html.Raw()中以防止 & 的编码。

或者只有Url.Action()控制器/动作位并将两个参数作为发布数据而不是直接在 url 上传递,jQuery 应该以这种方式为您整理 & 。

于 2012-01-03T11:36:17.567 回答
14

我认为您的问题在于Model.meta.PostAction-该属性是 astring吗?

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

  • 剃刀:@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);
于 2012-01-03T11:44:12.683 回答