c# - How do I Call a URL Action from Javascript? - Stack Overflow

I am doing:var url = '@Url.Action("Attachments", "Transactions")';url +=

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,

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,
Share Improve this question edited Jun 19, 2015 at 8:28 hutchonoid 33.3k15 gold badges101 silver badges106 bronze badges asked Jun 19, 2015 at 7:43 SamIAmSamIAm 2,5117 gold badges38 silver badges55 bronze badges 5
  • what are you trying to do? .load() - Load data from the server and place the returned HTML into the matched element api.jquery./load – Jossef Harush Kadouri Commented Jun 19, 2015 at 7:46
  • Put a console.log(url); statement immediately after var url = ... and check what it returns. – user3559349 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. – ramiramilu Commented Jun 19, 2015 at 7:54
  • Note also it should be url += '?id=' + 3201; (no forward slash) – user3559349 Commented Jun 19, 2015 at 7:56
  • Where are you 'doing' the javascript? What's the filename? Does it end in .js ? – fdomn-m Commented Jun 19, 2015 at 8:38
Add a ment  | 

2 Answers 2

Reset to default 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.

You Need to use Html.Raw check below

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744167874a4561392.html

相关推荐

  • c# - How do I Call a URL Action from Javascript? - Stack Overflow

    I am doing:var url = '@Url.Action("Attachments", "Transactions")';url +=

    9天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信