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:
load('@Url.Action("Links", "PartialAccount")', { beginDate: d1, endDate: d2})
because this will encode the parameters in d1
and d2
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:
/bar/foo?beginDate=1-1-2001&endDate=2-2-2001
(note that the /'s in the date will be encoded)
You cannot use this if you want your url to be:
/bar/foo/1-1-2001/2-2-2001
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 /).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With