Camillia
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
2. In AngularJS controller, I have this
3. In C# controller
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
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>
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;
};
3. In C# controller
// [HttpPost]
public ActionResult Index(string formId)
{
ViewBag.FormId = formId;
return View();
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
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.
ASKER
oh yeah, I think you're right. Totally forgot about that. I think that's it. I'll check it
ASKER
Thanks. I'll look at why the other route doesn't work.
You are welcome.
ASKER
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