I have a question about jQuery + Razor.. I want to build a javascript variable using razor with @Url.Action and the html attributes will be values of inputs. Like this:
var d1 = $('#d1').val();
var d2 = $('#d2').val();
var url = "@Url.Action("Links", "PartialAccount", new { beginDate = "d1", endDate = "d2" })"
$("#links").fadeOut("slow").load(url).fadeIn("slow");
How can I do this?
Is it the same that below?
var url = "/PartialAccount/Links/?beginDate=" + d1 + "&endDate=" + d2;
You should use the
load
method as proposed by InfernalBadger:because this will encode the parameters in
d1
andd2
correctly. But only if you can pass the parameters as part of the querystring, not when they should be a part of the url itself.So you can use if you want your url to be:
(note that the /'s in the date will be encoded)
You cannot use this if you want your url to be:
The
@Url
method, with the beginDate and endData parameters, does not work, because this is executed on the server, before the page is send to the browser.And your method by using the
+
operator fails the user enters "special characters" in the input boxes (like dashes /).I would pass the two values via the data parameter of load() if you want to get the values from dom elements?