Search code examples
c#asp.netasp.net-mvcasp-net-mvc-1

How to pass params to Action Method from ASPX based view using window.location.href


I am trying to pass string param to Action Method using Window.location.href but its not getting passed. I am having aspx based view without codebehind. Action Method gets called but with no param value. Developer tools showing params with Status Cancelled. Please Help (Using MVC 1)

//My AccrualController

public partial class AccrualsController : BaseController 
{
 public ActionResult Test(string YearAndMonth)
    {
           //Code
    }
}

//This is how i am passing params from javascript function in Index.aspx view

 window.location.href = "Accruals/Test?YearAndMonth=testvalue";

Solution

  • Tested the code below in an ASP.NET MVC 5 application and it works fine.

    Controller:

    public class AccrualsController : Controller
    {
        public ActionResult Test(string YearAndMonth)
        {
            return View();
        }
    }
    

    Home Index View:

    <a id="btn" href="#" class="btn btn-primary">Click Me</a>
    
    <script>
    
        var btn = document.getElementById("btn");
    
        btn.addEventListener("click", function (e) {
            e.preventDefault();
    
            window.location.href = "Accruals/Test?YearAndMonth=testvalue";
        });
    
    </script>
    

    Suspect it may be something to do with the way ASP.NET MVC 1.0 is configured. Which is going to be hard to work out because ASP.NET MVC 1.0 is now over 10 years old, documentation is sparse and many people will have moved on to newer versions.

    Bottom line

    You are using an incredibly old version of ASP.NET MVC and should consider if possible upgrading to ASP.NET MVC 5, where the code you have shown will work