0

I try to add $(this).prev("input").val() in my id argument for an API route, but I don't know how to stop the C# part , I thought about <text></text> or @: but doesn't work like this.

Anyone has a trick ? (thought about writting the URL instead of using the RouteURL function but I don't think it's a good way to program)

$.ajax({
   url: '@Url.RouteUrl("IsTranslated", new { id = <text>$(this).prev("input").val()</text> }, new {[email protected] })

 // [...]
})
2
  • You can't mix razor, asp tags, and JavaScript like this. They run at completely different times. Commented Jun 1, 2017 at 8:08
  • ok so I am gonna write the full URL for now Commented Jun 1, 2017 at 8:09

1 Answer 1

3

You can't inject a JavaScript value like that (remember, the Razor code is evaluated server-side). What you can do is use your helper to build a "template" string, and replace the id value:

var template = '@Url.RouteUrl("IsTranslated", new { id = 0 }, new { [email protected] })';

var id = $(this).prev("input").val();

var url = template.replace('id=0', 'id='+id);

$.ajax({
   url: url
   // [...]
})

This assumes, of course, that a querystring value is generated by the helper. If not, this might work:

var template = '@Url.RouteUrl("IsTranslated", new {}, new { [email protected] })';

var id = $(this).prev("input").val();

var url = template.replace('IsTranslated', 'IsTranslated?id='+id);

$.ajax({
   url: url
   // [...]
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.