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:
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 }) - add url html element data , read javascript
datamethod.
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
Post a Comment