Url.Action puts an & in my url, how can I solve this?

Niek de Klein picture Niek de Klein · Jan 3, 2012 · Viewed 36.7k times · Source

I want to send the variables itemId and entityModel to the ActionResult CreateNote:

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

with this javascript:

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

However, the url being send is

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

I want to send

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

How can I prevent Url.Action to put the & in front of the second variable that I want to send?

Answer

K. Bob picture K. Bob · Jan 3, 2012

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.