2016-09-07 3 views
0

投稿時にAngularjsを使用してオブジェクトをasp mvcコントローラに送信しようとしましたが、値はnullになります。

ここに何がありますか?

マイコントローラ:

public class RelatorioIndiceController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    public JsonResult GerarRelatorio(RelatorioFiltroPesquisa filtro) 
    { 
     throw new NotImplementedException(); 
    } 
} 

マイパラメータクラス:

public enum TipoRelatorio 
{ 
    IndiceCapacidade, 
    ImpactoMedio, 
    TempoMedio 
} 

public class RelatorioFiltroPesquisa 
{ 
    public TipoRelatorio TipoRelatorio { get; set; } 
    public string NomeRelatorio { get; set; } 
    public string Data { get; set; } 
} 

そして、私の意見:私は、オブジェクトが同じプロパティ名と、それらを持っている場合はチェックしました

<div ng-app="mainApp" id="body"> 
    <div ng-controller="myCtrl" id="form"> 
     <div class="radio"> 
      <label> 
       <input type="radio" ng-model="Filtro.TipoRelatorio" value="0" name="TiposRelatorio" /> 
       Índice Capacidade 
      </label> 
     </div> 
     <div class="radio"> 
      <label> 
       <input type="radio" ng-model="Filtro.TipoRelatorio" value="1" name="TiposRelatorio" /> 
       Impacto Médio 
      </label> 
     </div> 
     <div class="radio"> 
      <label> 
       <input type="radio" ng-model="Filtro.TipoRelatorio" value="2" name="TiposRelatorio" /> 
       Tempo Médio 
      </label> 
     </div> 
     <div class="input-group"> 
      <label>Nome Relatório</label> 
      <input type="text" ng-model="Filtro.NomeRelatorio" class="form-control" /> 
     </div> 
     <div class="input-group"> 
      <label>Data</label> 
      <input type="text" ng-model="Filtro.Data" class="form-control" placeholder="__/__/____" /> 
     </div> 
     <input type="submit" ng-click="Submit()" value="Gerar Relatório" /> 
    </div> 
</div> 


@section scripts 
{ 
    <script src="~/Scripts/angular.min.js"></script> 

    <script> 
     angular.module('mainApp', []) 
     .controller('myCtrl', ['$scope', '$http', function ($scope, $http) { 

      $scope.Submit = function() { 
       $http({ 

        method: "GET", 
        url: "/RelatorioIndice/GerarRelatorio/", 
        params: { filtro: $scope.Filtro } 

       }) 
       .success(function (data) { 
        console.log("Sucesso"); 
       }); 
      } 

     }]); 
    </script> 
} 

大丈夫です、コピーして貼り付けました。私はすでに別の類似の質問をここで確認していますが、この問題に関して私は助けてくれませんでした。

public JsonResult GerarRelatorio(FiltroRelatorioPesquisa filtro) 
{ 
    ... 
    return Json(result, JsonRequestBehavior.AllowGet); 
} 
:あなたが要求を取得できるようにする必要があり、コントローラで

$scope.submit = function() { 
$http.get("/RelatorioIndice/GerarRelatorio/", $scope.Filtro) 
    .success("Sucesso"); 
}; 

:このバージョンに変更

$http({ 
    method: "GET", 
    url: "/RelatorioIndice/GerarRelatorio/", 
    params: { filtro: $scope.Filtro } 
     }) 
     .success(function (data) { 
      console.log("Sucesso"); 
     }); 

は、問題を解決した:それは問題が起こった

+0

コードをコピーして貼り付けても問題なく動作しました。あなたはどこでヌルになっていますか? – Shyju

+0

私はコントローラのアクションでnull値を取得しています。 – Trepid

答えて

1

は、このコードとありました

古いコードは動作しますが、以前のバージョンのAngular(私はsuではありません私たちは私の仕事で使っています。そこに私はそのsintaxに慣れている)。 1.5.8については、新しいコードが有効でした。

+0

興味深い:F12開発ツールを押してネットワーク通信を見ると、httpメッセージが '$ http.get'と' $ http({method: "GET"、...)の間でどのように異なっているかを見ることができます。 –

+0

私はそれを確認します。テスト中は、整数をparamとして送信しようとしたが、複雑なオブジェクトでは送信できなかった。私は私の仕事で自分のコードをチェックしていましたが、あなたが提案したそのシタックスを使って解決策を使用しました。 – Trepid

+0

SOに従う方法はありますか?私はあなたに従いたいと思います。 =) – Trepid

関連する問題