Send view parameters to the controller With angularjs

0
DropDownList 

was created by the framework

@Html.DropDownList("IdPais", null, "Seleccione un país", htmlAttributes: new { @class = "form-control", id = "SelectPais", onchange = "SelPais()" })

This form sends me the id that I need to the controller. the problem is how to send when I use angular?

<div class="col-lg-10">
    <select class="form-control" id="SelectDepa" onchange="SelDepa()">
        <option value="">seleccione Departamento</option>
        <option ng-repeat="item in myDataDepa" ng-value="item.IdDepartamento">{{item.Nombre}}</option>
    </select>
</div>

my controller

  public ActionResult Imprimir(Parameters parametros)
        {
//**
}

my model

public class Parameters
    {
        public int IdPais { get; set; }
        public int IdDepartamento { get; set; }
}
    
asked by 29.01.2018 в 15:00
source

1 answer

2

Use the directive ngOptions to assign values to a select. This is assigned a ngModel assigned to the object of the selected value in select :

angular.module("app",[])
.controller("ctrl",function($scope, $http){

  $scope.myDataDepa = [
    { IdDepartamento: 1, Nombre: "Departamento 1" },
    { IdDepartamento: 2, Nombre: "Departamento 2" },
    { IdDepartamento: 3, Nombre: "Departamento 3" }
  ];
  
  $scope.departamento = {};
  $scope.guardar = function(){
    console.log($scope.departamento);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
    <select ng-options="departamento.Nombre for departamento in myDataDepa" ng-model="departamento">
    </select>
    
    <input type="button" ng-click="guardar()" value="Guardar" />
</div>

Note how pressing save prints the selected value. Now you only need to send those values by ajax using the service $http . For example:

$http.post("url", $scope.departamento).success(function(response){
   // respuesta del servidor.
});
    
answered by 29.01.2018 / 15:29
source