2016-10-11 3 views
2

Angular JS 스크립트에서 ASP.NET 웹 API 메서드를 호출하려고합니다. HTTP 요청은 POST 유형이며, 일부 데이터를 웹 API 메소드로 보내려고합니다.웹 API 호출 후 개체 매개 변수가 항상 null입니다.

여기에 내 AngularJS와 코드

$http({ 
     url: '/api/StudentsBasicInfo/InsertOrUpdateStudentBasicInfo', 
     method: "POST", 
     data: JSON.stringify(d), 
     contentType: "application/json;charset=utf-8", 
     dataType: 'json' 
    }).success(function (data, status) { 
     if (data == "\"success\"") { 

    }); 

(가) 자바 스크립트 코드 위 D 간단한 JSON의 목적이다. 여기

내 웹 API 아래

 [ActionName("InsertOrUpdateStudentBasicInfo")] 
     public string InsertOrUpdateStudentBasicInfo([ModelBinder] StudentDynamicFormVM student) 
     { 
      try 
      { 

       StudentsBasicInfoBL studentsBasicInfoBL = new StudentsBasicInfoBL(); 

       long returnedId = studentsBasicInfoBL.InsertStudentsBasicInfoAndEnrolledProgram(student); 

       if(returnedId > 0) 
       { 
        return "success"; 
       } 
       else 
       { 
        return "error"; 
       } 
      } 
      catch(Exception ex) 
      { 
       return "error"; 
      } 
     } 

이다 나는이 코드를 실행하려고 할 때 내가

그러나
public class StudentDynamicFormVM 
{ 

    public long SerialId { get; set; } 

    public long StudentSerialId { get; set; } 

    public string FirstName { get; set; } 

    public string MiddleName { get; set; } 

    public string LastName { get; set; } 

    public int ProgramTypeId { get; set; } 

    public string AdmittedSemester { get; set; } 

    public string AdmittedDate { get; set; } 

    public int DepartmentId { get; set; } 

    public int ProgramId { get; set; } 

    public string MaritalStatus { get; set; } 

    public string Nationality { get; set; } 

    public string BloodGroup { get; set; } 

    public string Country { get; set; } 

    public string Religion { get; set; } 

    public int Mobile { get; set; } 

    public string Email { get; set; } 

    public string NationalId { get; set; } 

    public string PresentAddress { get; set; } 

    public string PermanentAddress { get; set; } 

    public string IdentificationMark { get; set; } 

    public string ColourOfHair { get; set; } 

    public string DateofBirth { get; set; } 
} 

에 HTTP 요청 데이터를 바인딩하기 위해 노력하고 강력한 형식의 C# 객체 내 웹 API가 올바르게 호출되고 (디버깅으로 확인 됨), 이지만 스크린 샷과 같이 모델 클래스의 모든 속성은 null입니다. 이 게시물 (Web API complex parameter properties are all null)의 제안에 따라

enter image description here

, 나는이

 [HttpPost] 
     [ActionName("InsertOrUpdateStudentBasicInfo")] 
     public string InsertOrUpdateStudentBasicInfo(object model) 
     { 
      try 
      { 

       var jsonString = model.ToString(); 
       StudentsInfoHybrid student = JsonConvert.DeserializeObject<StudentsInfoHybrid>(jsonString); 


       StudentsBasicInfoBL studentsBasicInfoBL = new StudentsBasicInfoBL(); 

      } 
      catch(Exception ex) 
      { 
       return "error"; 
      } 
     } 

내 웹 API 코드를 변경하지만 내가 JSON 객체가 올바르게 웹에 의해 수신되는 것을보고 API를 사용하지만 직렬화를 시도한 후에 객체 매개 변수가 null이되었습니다. 즉 이전과 같은 결과입니다 (단, 예외는 발생하지 않았습니다).

나는 각도 JS 코드에서 JSON.stringify 메소드 호출을 생략하고 POST 요청에서 contentType 헤더를 제거하려고 시도했습니다. 그러나 결과는 완전히 동일하게 유지되었습니다.

기술 스택 ASP.NET 4.5 웹 API 각도 JS 1.2.19 비주얼 스튜디오 2015

마지막으로, 나는 구글 크롬의 네트워크 로그의 스크린 샷을 첨부하고

enter image description here

답변

1

변경 WebAPI에있는 속성의 이름을 보내려는 속성의 이름으로 바꿉니다. Academic_DetailsStatic이라는 개체를 보내는 것처럼 보입니다.

또는 게시중인 개체의 이름을 WebAPI의 모델 바인딩 속성 이름과 일치하도록 변경하십시오.

+0

감사합니다. @jle !! javascrit 끝 (즉, 중첩 된 객체)에서 Academic_DetailsStatic이라는 속성에 모든 속성이 래핑 된 것처럼 보입니다. 나는 d .Academic_Details를 보냈다. Static to Web API가 효과적이었다. – Shuaib

관련 문제