Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

AngualrJS and C# Code works but another way of doing this

I have a side menu and I click on a link. I want to redirect the user to another page and pass a parameter.

1. In Razor, I do this

 <button ng-click="openForm(row.Id)"  class="btn btn-primary btn-border btn-rounded-20 btn-ef btn-ef-4 btn-ef-4b mb-10">{{row.FormName}}<i class="fa fa-arrow-right"></i></button>

Open in new window


2. In AngularJS controller, I have this
 $scope.openForm = function (formId) {

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

            //$http({
            //        method: 'POST',
            //        url: '/displayform/index',
            //        data: JSON.stringify(formId),
            //        contentType: "application/json",
            //        dataType: "json"

            //}
               
             

            //    ).then(function () { });

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

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

        };

Open in new window


3. In C# controller
 // [HttpPost]
        public ActionResult Index(string formId)
        {

            ViewBag.FormId = formId;
            return View();
        }

Open in new window


4. The URL looks like this when the user is redirected
http://localhost:63410/displayform/index/?formId=1

Is there a better way of doing this? I wanted the URL to look like this but couldn't get it working...the passed parameter comes out as null http://localhost:63410/displayform/index/1
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Camillia

ASKER

any reason why you are not doing this in angular
The way I learned it at work is we use MVC routing not angular. That's the reason.

Secondly, what format is your view expecting (in terms of the url)
a) parameter passed as ?index=1 (normal query parameters)
b) parameter passed as part of the path /index/1

I would like it to be /index/1 because I think it's cleaner. But if having it as ?index=1 is ok, then I'll leave it as is.

Code works as I have it now. With Angular and MVC/C#. I just wanted to know if having ?index=1 is OK or should I work on getting the URL like /index/1
This is your choice - from a programming perspective it is 6 of one half a dozen of the other. The only reason for doing the index/1 over ?index=1 is pretty urls for the user's benefit.
Thanks, I'll leave it as is. I couldn't read the "1" value in MVC's controller. Would come out as null ... that's why I went with ?index=1
It depends how you have set up your view - not the sharpest tool in the shed when it comes to .Net - but if I remember correctly when you setup your routes you specify how the path should be interpreted - but wouldn't be able to tell you how to do that off the top of my head.
oh yeah, I think you're right. Totally forgot about that. I think that's it. I'll check it
Thanks. I'll look at why the other route doesn't work.
You are welcome.