Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

asp.net mvc - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
<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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...