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

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

Who is Participating?
Snarf0001Connect With a Mentor Commented:
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.
CamilliaAuthor Commented:
oh, let me see.
CamilliaAuthor Commented:
that was it.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.