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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Snarf0001Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CamilliaAuthor Commented:
oh, let me see.
0
CamilliaAuthor Commented:
that was it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.