Search code examples
c#jqueryasp.net-mvcjquery-post

JQuery post method is always redirecting to the Index() method in c# MVC and the parameters passed are not reflected


I am trying to call one of the action method defined in my controller when the value in the drop down is changed. I am also passing the changed value as parameter to the action method. I tried changing the action method name just to confirm but it always hits index() and the parameter passed is always received as null.

$('#dropdown').change(function () {
    var selectedValue = $('#dropdown').val();
    var url = "@Url.Action("Index","Controller")" + selectedValue;
    $.post(url);
});

public ActionResult Index(string id)
{
    //some code
}

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute(
    "Home",
    "Home",
    new { controller = "Home", action = "Index" }
);

Solution

  • @Url.Action("Index","Controller")" + "?id=" + selectedValue; worked. Thanks for all your help.