I have the following Javascript code:
var idJS;
$('.disp').click(function () {
idJS = $(this).attr("id");
})
And then I want to do this:
@Ajax.ActionLink("myMethod", "myMethod", "HomeController", new { id = idJS },
new AjaxOptions() { HttpMethod = "Get", UpdateTargetId = "myDiv" })
Is it possible to do this?
I have the following Javascript code:
var idJS;
$('.disp').click(function () {
idJS = $(this).attr("id");
})
And then I want to do this:
@Ajax.ActionLink("myMethod", "myMethod", "HomeController", new { id = idJS },
new AjaxOptions() { HttpMethod = "Get", UpdateTargetId = "myDiv" })
Is it possible to do this?
Share Improve this question asked May 11, 2012 at 14:44 mornanermornaner 2,4242 gold badges28 silver badges39 bronze badges 02 Answers
Reset to default 5This is not possible to do because Razor view code is rendered when the page it loaded, so all that code is resolved (transformed to HTML) before the javascript can even execute.
You can however use Javascript to change properties of a link, like the following for example (using JQuery though I am afraid, look up a javascript equivalent if need be)
$("a").attr("href", "/Home/myMethod/" + idJS);
ok I will look it up then, javascript only:
document.getElementById("link").href = "/Home/myMethod/" + idJS;
No. Server side and client side are two very different things. The server side is rendered before even reaching the client. By the time $(document).ready() is fired, the server is finished.
What you can do is change the id of the link with jQuery. With a little more information, I would be able to help more, but it's a simple thing to change attribute with jQuery.
I would set the id to be hard coded, like "ajaxBtn" or something, then, in your click even for .disp, change a data attribute to what you need it to be. However, without more data, I can't know for sure exactly why you're needing to set the id.