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&pos=0&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.
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 .
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.
<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>
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With