Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 Url.Action querystring generation

I am trying to generate an url for an MVC 3 action within javascript environment (in a cshtml file).

<script type="text/javascript">
  ...
  var src = "@Url.Action("GetProductImage", new { productId = Model.Product.Id, pos = 1, size = 0 })";
  $(document.createElement("img")).attr("src", src);
  ...
</script>

Now this works almost fine, my problem is that the querystring is being escaped. Instead of:

"/Products/GetProductImage?productId=1&pos=0&size=0"

it generates:

"/Products/GetProductImage?productId=1&amp;pos=0&amp;size=0"

so my action does not get called.

Now I know I can make my own custom Url helper function, but I was wondering if I can use this or some other built in helper to get the unescaped URL?

Thanks in advance, G.

like image 839
egyedg Avatar asked May 03 '11 15:05

egyedg


People also ask

What is a QueryString used for?

A querystring is a set of characters input to a computer or Web browser and sent to a query program to recover specific information from a database .

What is request QueryString in C#?

A Query String collection is a parsed version of the QUERY_STRING variable in the Server Variables collection. It enable us to retrieve the QUERY_STRING variable by name. When we use parameters with Request. QueryString, the server parses the parameters sent to the request and returns the effective or specified data.


2 Answers

<script type="text/javascript">
   var src = "@Html.Raw(Url.Action("GetProductImage", new { productId = Model.Product.Id, pos = 1, size = 0 }))";
   $(document.createElement("img")).attr("src", src);
</script>
like image 195
Govind Malviya Avatar answered Oct 02 '22 12:10

Govind Malviya


var src = "@Html.Raw(Url.Action("GetProductImage", new { productId = Model.Product.Id, pos = 1, size = 0 }))";

Url.Action worked for me not HtmlUrl.Action

Enjoy!

like image 23
Har Avatar answered Oct 02 '22 11:10

Har