2015-01-09 5 views
1

내 양식에서 웹 API 서비스에 개체를 게시하는 방법을 알아 내려고하고 있습니다. 컨트롤러 내에서 입력 값을 추가하려는 모델을 정의했습니다. 내 입력 필드 내에서WebAPI에 개체를 게시하는 방법

$scope.Label; 

나는 그들이 사용하는 바인딩이 ng-model 같은 :

<input type="checkbox" ng-model="label.isPublic" /> 
<input type="text" ng-model="label.labelName" required focus-me /> 
이 두 필드는 내 서비스에 전달 양식의 제출에

내 WebApi에 제출

이 제출을 두 가지 방법으로 시도했습니다.

function addLabel(label) { 
     var mylabel = encodeURIComponent(angular.toJson(label)); 
     return $http.post('reportLibrary/createlabel/', { params: label }, { 

     }).then(function (response) { 
      return response.data; 
     }); 
    }; 

및 als O 선언 파라미터 I가 포스트 방법 설정을 가지고 webAPI에서

function addLabel(label) { 
     var mylabel = encodeURIComponent(angular.toJson(label)); 
     return $http.post('reportLibrary/createlabel/', label , { 

     }).then(function (response) { 
      return response.data; 
     }); 
    }; 

없이 다음과 같이

ReportLabel (DTO)은로 정의된다
[Route ("reportLibrary/createlabel/")] 
     [HttpPost] 
     public DTOs.ReportLabel CreateLabel(DTOs.ReportLabel json) 
     { 
      DTOs.ReportLabel result = new DTOs.ReportLabel(); 

     //.... do stuff 
      return result; 
     } 

다음 :

public class ReportLabel 
{ 
    public Int64 LabelId { get; set; } 
    public string LabelName { get; set; } 
    public bool IsPublic { get; set; } 
    public IEnumerable<Report> Reports { get; set; }//placeholder? 

} 

을 문제는 내가 내 각도 서비스에서 객체를 게시 할 때 API 내에서 null로 표시된다는 것입니다. 메소드의 유형을 JToken 또는 JObject와 같은 것으로 변경하면 값이 나타납니다.

각도에서 가로 질러 전달되지 않는 유형을 정의 할 때 왜 이해할 수 있습니까?

감사합니다.

답변

1

추가 작업을 수행하는 것처럼 보입니다. 당신은

return $http.post('reportLibrary/createlabel/', { LabelId: 101, LabelName: 'myname' }, { 

다음

public DTOs.ReportLabel CreateLabel([FromBody]ReportLabel reportLabel) 

가 지나가는 네트워크 값에 봐 다음 JSON에서의 통과 JSON으로 인코딩 할 필요가 없습니다 당신은 디버그 툴에서 참조하거나 게시 된 실제를 피들러한다 값 (양식 값).

+0

이것은 실제로 작동하지만 이것은 약간 무거워 보입니다. 그 위에 10 개 또는 20 개의 속성을 가진 객체가 있으면 어떻게됩니까? 마치 객체를 전달하고 각 속성/필드를 선언하고 설정하는 방법이 있어야하는 것처럼 보입니다. 나는 다른 옵션을보고 싶습니다. – rlcrews

+0

내 관심사는 너무 무거워서 마술처럼 느껴지지 않습니다. Microsoft는 데이터를 전달한 다음 해당 데이터에 매핑하려고 시도하여 쉽게 확인하려고합니다. 매개 변수 전달과 관련하여 많은 성공을 거둔 적이 없지만 가능하다는 것을 알고 있습니다. 내가 완전한 컨트롤을 원할 때 나는 폼 콜렉션을 사용한다. (나는 이것을 여기에 올렸다 : http://peterkellner.net/2013/08/18/collection-form-post-parameters-in-webapi-controller/) –

관련 문제