Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 376
Niek de Klein Avatar asked Jan 03 '12 11:01

Niek de Klein


People also ask

What does url Action?

A URL action is a hyperlink that points to a web page, file, or other web-based resource outside of Tableau. You can use URL actions to create an email or link to additional information about your data. To customize links based on your data, you can automatically enter field values as parameters in URLs.

How do I set an area in URL action?

You can use this Url. Action("actionName", "controllerName", new { Area = "areaName" });

What is difference between HTML ActionLink and URL action?

Yes, there is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.


2 Answers

I didn't notice yesterday that you had &amp; 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.

like image 139
K. Bob Avatar answered Oct 16 '22 22:10

K. Bob


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);
like image 22
Keith Avatar answered Oct 17 '22 00:10

Keith