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)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

           
            routes.MapRoute(
                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) {

            //console.log(JSON.stringify(formId))

          
            window.location = "/displayform/index/?formId=" + formId; //https://stackoverflow.com/questions/37031143/asp-net-angularjs-how-to-click-on-link-and-route-to-controller-method-with-id

        };

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



window.open('/displayform/index/' + formId, '_self', false);

Open in new window

LVL 8
CamilliaAsked:
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.
0
 
CamilliaAuthor Commented:
oh, let me see.
0
 
CamilliaAuthor Commented:
that was it.
0
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.