Need to send hidden input value to mvc controller using angularjs

I have a hidden input field that contains the value that I need to send to 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:

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

How can I send a value to my controller and how to get it in the controller? This is the method that I used for the MVC controller.

[HttpPost]
public JsonResult GetProjectsList()
{
    return Json();
}
+4
source share
4 answers

Have you tried using the 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');
 })

;

+1
source

Hope this helps 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
   }
})
+1
source

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

. , , .

+1

, AngularJS , . :

  • :

ng-, , , id:

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

Javascript :

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

Angular hidden, type=text display:none:

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

javascript, , ng-model:

var project_id = $scope.projectID;
0

Source: https://habr.com/ru/post/1619791/


All Articles