在我的 Url 中,Action 放入一个 & ,我该如何解决这个问题呢?

我想将变量 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。将 & 放在要发送的第二个变量前面的操作?

39786 次浏览

I didn't notice yesterday that you had & I thought that was the SO editor had changed that. Try wrapping your Url.Action() in a @Html.Raw() to prevent the Encode of &.

Or alternatively only Url.Action() the controller/action bit and pass the two parameters as post data rather than directly on the url, jQuery should sort out the &'s for you that way.

I think your problem is with Model.meta.PostAction - is that property a string?

If so then my guess would be that you're adding it to the page with either:

  • Razor: @Model.meta.PostAction
  • ASP view engine: <%:Model.meta.PostAction%>

Both of which automatically encode that string for you.

To fix it either use @Html.Raw()/<%= (both of which don't encode) or make the PostAction property an IHtmlString that knows that it's already been encoded:

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