Angular and MVC - One code works, one doesn't - Why is this parameter coming out as null

Camillia used Ask the Experts™
1. I have this in MVC routeconfig
 public static void RegisterRoutes(RouteCollection routes)

                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }


Open in new window

2. I have this Controller

  // [HttpPost]
        public ActionResult Index(string formId)

            ViewBag.FormId = formId;
            return View();

Open in new window

3. This works and when I get to the controller, I see "formId"'s value. The URL shows as ?/index/1

  $scope.openForm = function (formId) {


            window.location = "/displayform/index/?formId=" + formId; //


Open in new window

How come these don't work...I step thru it and 'formId" comes out as null. I added HttpPost as well to the MVC Controller. what am I missing?

 window.location = "/displayform/index/"+ formId;

Open in new window'/displayform/index/' + formId, '_self', false);

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
It's the naming of the variables.

In the first example, you're using specifically named querystring params, so the routing engine has no problem picking out and matching the "formid" parameter.

In the second one, where you're using the path based routing "/displayform/index/{formid}", the route specified in Register is expecting SPECIFICALLY "id", not "formid".
If you changed your function to expect that instead:

public ActionResult Index(string id)

Open in new window

It would pass the value in.
oh, let me see.
that was it.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial