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


i doing:

var url = '@url.action("attachments", "transactions")'; url += '/?id=' + 3201; $("#attachments").load(url); 

however, on load doesn't anything. missing something?

i want call similar to:

@{html.renderaction("attachments", "transactions", new { id = 3301 });} 

i following error on console:

http://server:54137/transactions/@url.action(%22attachments%22, 

you must using external javascript file not parse razor syntax hence error in console of @url.action(%22attachments%22..

you have couple of options:

  1. create javascript function , pass in url:

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

then in razor call within script tag:

loadurl(@url.action("attachments", "transactions", new { id = @model.id }) 
  1. add url html element data , read javascript data method.

in razor markup add this:

<button data-url="@url.action("attachments", "transactions", new { id = @model.id })" /> 

from javascript event handler read with:

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

i prefer second option.


Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -