1

I am doing:

var url = '@Url.Action("Attachments", "Transactions")';
url += '/?id=' + 3201;
$("#attachments").load(url);

However, on load it doesn't do anything. Am i missing something?

I essentially want to call something similar to:

@{Html.RenderAction("Attachments", "Transactions", new { id = 3301 });}

I get the following error on console:

http://server:54137/Transactions/@Url.Action(%22Attachments%22,
5
  • what are you trying to do? .load() - Load data from the server and place the returned HTML into the matched element api.jquery.com/load Commented Jun 19, 2015 at 7:46
  • Put a console.log(url); statement immediately after var url = ... and check what it returns. Commented Jun 19, 2015 at 7:52
  • 1
    your code works fine for me. I used console.log(url); and it printed - /Transactions/Attachments/?id=3201. So please check the other parts of your code. Commented Jun 19, 2015 at 7:54
  • Note also it should be url += '?id=' + 3201; (no forward slash) Commented Jun 19, 2015 at 7:56
  • Where are you 'doing' the javascript? What's the filename? Does it end in .js ? Commented Jun 19, 2015 at 8:38

2 Answers 2

5

You must be using an external JavaScript file which will not parse your razor syntax hence the error in your console of @Url.Action(%22Attachments%22..

You have a couple of options:

  1. Create a JavaScript function and pass in the url:

    function loadUrl(url) { $("#attachments").load(url); }

Then in your razor call it within a script tag:

loadUrl(@Url.Action("Attachments", "Transactions", new { id = @Model.Id })
  1. Add the url to the html element as data and read it from your JavaScript with the data method.

In your razor markup add this:

<button data-url="@Url.Action("Attachments", "Transactions", new { id = @Model.Id })" />

From your JavaScript event handler read it with:

var url = $(this).data('url');
$("#attachments").load(url);

I prefer the second option.

Sign up to request clarification or add additional context in comments.

5 Comments

For some reason, it doesn't seem to worK? I get the same error when I try option two. I have attached a data value to the option select list and was able to get the url out properly. Then when I try to load it, it still gives me the error of:
@SamIAm Hi, you need to make sure that you are calling @Url.Action from within the razor file i.e. .cshtml or it will not resolve. If you are calling it from an external .js file it will just treat it as javascript and resolve as @Url.Action rather than a path to the action i.e. /transactions/attachments.
@hutchonoid How can we use this inside a .js file ?
Hi @Sebastian, yes you can. You have a couple of options, create a function with the url as the parameter and pass it in via i.e. loadThisUrls(@Url.Action....) to resolve the path or just bundle it up as is and resolve the url as shown above without passing it in.
1

You Need to use Html.Raw check below

var url = "@Html.Raw(Url.Action("Attachments", "Transactions"))";
url += '/?id=' + 3201;
$("#attachments").load(url);

1 Comment

You do not need to use Html.Raw() - '@Url.Action("Attachments", "Transactions")'; will return "/Transactions/Attachments"

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.