I want to pass a parameter from a view to an action through the following jQuery code, but I don't succeed to pass the parameter. Am I missing something?
$('li').click(function () {
employeeID = $(this).attr('id');
window.location.href = '/ApproveWork/GetEmployeeDays/'+employeeID;
});
Controller:
[HttpGet]
public ActionResult GetEmployeeDays(string userCode)
{
.....
return View("GetEmployeeDays", model);
}
routing from Global.asax
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
P.S. I tried to use AJAX jQuery function, but this doesn't return me the view that I specified in the Action.
I want to pass a parameter from a view to an action through the following jQuery code, but I don't succeed to pass the parameter. Am I missing something?
$('li').click(function () {
employeeID = $(this).attr('id');
window.location.href = '/ApproveWork/GetEmployeeDays/'+employeeID;
});
Controller:
[HttpGet]
public ActionResult GetEmployeeDays(string userCode)
{
.....
return View("GetEmployeeDays", model);
}
routing from Global.asax
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
P.S. I tried to use AJAX jQuery function, but this doesn't return me the view that I specified in the Action.
Share Improve this question edited Jan 18, 2013 at 13:22 pisi asked Jan 18, 2013 at 11:34 pisipisi 1271 gold badge4 silver badges16 bronze badges 3 |4 Answers
Reset to default 9Is your javascript on the view page
If so use the Url.Action helper to build the url in the normal way
var url = '@Url.Action("Action", "Controller", new {parametername = "REPLACEME"})';
window.location.href = url.Replace('REPLACEME', parameter_value);
Updated to account for client side variable *Update 2: updated typo*
Did you try with ajax?
$.ajax({
url: '@Url.Action("GetEmployeeDays", "ApproveWork")',
type: 'GET',
dataType: 'html',
contentType: 'application/json; charset=utf-8',
data: { userCode: $(this).attr('id') }
})
Can you please check your Global.asax file the register routes?
Please check the optional variable name over there.
Suppose you have register route using following.
routes.MapRoute( _
"Default", _
"{controller}/{action}/{id}", _
New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional} _
)
You have to write your action below way.
[HttpGet]
public ActionResult GetEmployeeDays(int id)
{
.....
return View("GetEmployeeDays", model);
}
or
[HttpGet]
public ActionResult GetEmployeeDays(string id)
{
.....
return View("GetEmployeeDays", model);
}
You cannot pass parameter with the help of @Url.Action
with jquery.
You can create initial string containing url and then you can pass that string to window.location.href
$_BaseUrl="Your application url";
<script>
Window.location.href = $_BaseUrl + '/Admin/Account/SwitchSuperAdminLogin?UserName=' + user + '&Password=' + pass + '&IndustryId=' + indus;
</script>
parameter_to_pass
defined and what is it's value? Also, seeing your action method may help. – Rory McCrossan Commented Jan 18, 2013 at 11:36