Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have a hidden input field that contains a value I need to send my mvc controller.

$http({ method: 'GET', url: '/User/GetProjectsList' })
.success(function (data, status, headers, config) {         
    $scope.workflow = [];
    $scope.Projects = data;
})
.error(function (data, status, headers, config) {
    alert('error');
});

And the hidden field is:

<input type="hidden" ng-model='ProjectId' value="{{ProjectsObj.IDWorkflow}}"></input>

How can I send the value to the my controller and how do I get it in the controller? This is the method I had on the MVC controller.

[HttpPost]
public JsonResult GetProjectsList()
{
    return Json();
}
share|improve this question
    
directly attach the value to scope variable in angular so you can use it in the controller. – Anway Kulkarni Dec 14 '15 at 5:53
    
in the angular your do not need to have a hidden input.. but why? because we have controllers to connect to our API controllers when you want to send a command to your API controller your a model in your model put your hidden input value. – Maher Dec 14 '15 at 5:54

Have you tried with query string ?

var pID=$scope.ProjectId
$http({ method: 'GET', url: '/User/GetProjectsList?ProjectID='+pID}).
    success(function (data, status, headers, config) {         
        $scope.workflow = [];

        $scope.Projects = data;

    }).
 error(function (data, status, headers, config) {
     alert('error');
 })

;

share|improve this answer

I hope this helped you ;)

 app.controller("myCtrl", function($scope) {
   $scope.formDetails = {};
   $scope.sendToApi = function(){
       var model = {
          id: $scope.formDetails.id //this is my hidden input
          name: $scope.formDetails.name,
       }
       //and then send your model to API
   }
})
share|improve this answer

It seems like AngularJS does not take in consideration hidden input when submitting forms, check this issue for more details. But you can get your value in 2 ways:

  1. classic way:

remove ng-model since has no sens with hidden type as explained above and add id:

<input id="project_id" type="hidden" value="{{ProjectsObj.IDWorkflow}}"/>

in Javascript side you simply do:

var project_id = document.getElementById('45').value;
  1. AngularJS way

since Angular ignores hidden elements, you can replace it with type=text and display:none:

<input type="text" name="project_id" ng-model="projectID" style="display: none;"/>

and your javascript side using two way data-binding you can acces your ng-model variable:

var project_id = $scope.projectID;
share|improve this answer

 @Html.HiddenFor(m => m.ProjectId)

You Can Simply Pass Hidden Value To Controller From View By using model value like above syntax.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.