I have below javascript function in my MVC application,
function EditProducts(productId, orderId, employeeId, mode)
{
mode = "edit";
debugger;
var url = '@Url.Action("Index", "Home", new { productId = "__productId__", orderId = "__orderId__", employeeId = "__employeeId__", Mode = "__mode__"})';
var params = url.replace('__productId__', productId).replace('__orderId__', orderId).replace('__employeeId__', employeeId).replace('__mode__', mode);
window.location.href = params;
}
But it doesn't work. Here is my controller code by I am not getting any values in below vaiables,
public ActionResult Index(int productId, int orderId, int employeeId, string mode)
{
return View();
}
Any ideas on how to pass multiple parameters through url.action?
Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".
You can concat the client-side variables with the server-side url generated by this method, which is a string on the output. Try something like this: var firstname = "abc"; var username = "abcd"; location. href = '@Url.
Yes, completely possible. And, it can be multiple views, or even a FileResult or other result type.
Use @Html.Raw to prevent the ampersand from being converted to &
inside javascript code
function EditProducts(productId, orderId, employeeId, mode)
{
mode = "edit";
debugger;
var url = '@Html.Raw(Url.Action("Index", "Home", new { productId = "__productId__", orderId = "__orderId__", employeeId = "__employeeId__", Mode = "__mode__"}))';
var params = url.replace('__productId__', productId).replace('__orderId__', orderId).replace('__employeeId__', employeeId).replace('__mode__', mode);
window.location.href = params;
}
Get the base url to the action method using Url.Action
helper method and add the querystring params to that.
This should work fine
$(function(){
var productId = 23;
var employeeId = 44;
var orderId = 34;
var mode = "tes";
var url = '@Url.Action("Index", "Post")';
url += '?productId=' + productId + '&orderId=' + orderId +
'&employeeId=' + employeeId + '&mode=' + mode;
window.location.href = url;
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