2017-12-13 60 views
0

Kendo Grid 인라인 편집 CRUD를 구현하는 중에 오류가 발생했습니다. 새 레코드를 추가하고 업데이트하면 오류가있는 응답 코드 500이 표시됩니다."잘못된 JSON 프리미티브 : 모델"오류 컨트롤러에 모델을 전달하는 동안 Kendo Grid 데이터 소스에서

나는이 문제가 parameterMap에서 발생한다고 생각합니다. Kendo의 컨트롤러에 모델을 전달하는 올바른 방법은 무엇입니까?

Invalid JSON primitive: models.

모델은 무엇입니까?

소스 코드 :

$(document).ready(function() { 
     var baseUrl = "/SomeUrl", 
      dataSource = new kendo.data.DataSource({ 
       transport: { 
        read: { 
         url: baseUrl + "/GetAccolades?profileId=" + @profileId, 
         type: "GET", 
         dataType: "json" 
        }, 
        create: { 
         url: baseUrl + "/AddAccolade", 
         type: "POST", 
         dataType: "json", 
         contentType: "application/json", 
        }, 
        update: { 
         url: baseUrl + "/UpdateAccolade", 
         type: "PUT", 
         dataType: "json", 
         contentType: "application/json", 
        }, 
        delete: { 
         url: baseUrl + "/DeleteAccolade", 
         type: "DELETE", 
         dataType: "json" 
        }, 
        parameterMap: function (options, operation) { 
         if (operation !== "read" && options.models) { 
          alert(kendo.stringify(options.models)); 
          return { models: kendo.stringify(options.models) }; 
         } 
        } 
       }, 
       batch: true, 
       schema: { 
        model: { 
         id: "ProfileAccoladeID", 
         fields: { 
          ProfileAccoladeID: { editable: false, nullable: false }, 
          ProfileID: { editable: false, nullable: false, defaultValue: @profileId }, 
          Years: { type: "string" }, 
          Name: { type: "string" }, 
          Level: { type: "string" }, 
          Event: { type: "string" },        
         } 
        } 
       } 
      }); 

     $("#accolades-grid").kendoGrid({ 
      dataSource: dataSource, 
      pageable: false, 
      height: 550, 
      toolbar: ["create"], 
      columns: [ 
       { field: "Years", width: "150px" }, 
       { field: "Name", title: "Name", width: "150px" }, 
       { field: "Level", title: "Level", width: "150px" }, 
       { field: "Event", title: "Event", width: "450px" },      
       { command: ["edit", "destroy"], title: " ", width: "250px" }], 
      editable: "inline" 
     }); 
    }); 
</script> 

컨트롤러 방법 :

[HttpPost] 
    public JsonResult AddAccolade(ProfileAccolade accolade) 
    { 
     using (var db = new XXXEntities()) 
     { 
      if (accolade != null) 
      { 
       var newAccolade = new ProfileAccolade() 
       { 
        ProfileID = accolade.ProfileID, 
        Years = accolade.Years, 
        Name = accolade.Name, 
        Level = accolade.Level, 
        Event = accolade.Event 
       }; 
       db.ProfileAccolades.Add(newAccolade); 
       db.SaveChanges(); 

       return Json(new { Success = true }); 
      } 
      else 
      { 
       return Json(new { Success = false, Message = "Error occured" }); 
      } 
     } 
    } 

가 어떻게이 오류를 수정합니까?

업데이트 : 오류 Invalid JSON primitive: models.contentType: "application/json",을 제거함으로써

가 없어. 그러나 컨트롤러는 모델을 얻지 못합니다.

해결 방법이 있습니까?

+0

'alert (kendo.stringify (options.models)) '에서 어떤 종류의 JSON 문자열 결과를 얻었습니까? 일반적으로이 문제는 JSON 문자열 형식 대신 URL 인코딩 된 매개 변수가있는 메서드를 호출 한 후에 발생합니다. –

+0

당신은 보셨나요? https://stackoverflow.com/questions/18695302/kendo-datasource-parameter-map – jwatts1980

+0

@Tetsuya Yamamoto 잘 형식화 된 JSON 문자열이 있습니다. –

답변

0

문제는 kendo.data.DataSourcetransport.createtransport.update 부분 둘 다 내부 contentType: "application/json"의 사용에 의해 시작되었다. 두 작업 모두 AJAX 호출 (예 : jQuery.ajax() 메소드)을 사용하기 때문에 contentType 설정은 요청 본문 유형을 보내는 방식에 영향을줍니다. "application/json" 콘텐츠 유형을 설정함으로써

요청 본문 JSON 컨텐츠로서 취급되지만, 실제로 transport.parameterMap 반환 URL은 models 함유 버전의 인코딩 된 키 - 값 쌍 (KVP) 등 JSON 문자열 포맷. 따라서 "잘못된 JSON 프리미티브" URL 인코딩 형식이 JSON 형식과 같지 않기 때문에 오류가 발생합니다.

당신이 JSON 데이터로 models의 URL 인코딩 형식을 변환, AJAX 호출을위한 "application/json" 설정을 유지 JSON.stringify 수단을 추가 한 경우 : 그러나 당신이 기본 콘텐츠 형식으로 parameterMap을 보내려면

parameterMap: function (options, operation) { 
    if (operation !== "read" && options.models) { 
     return JSON.stringify({ models: kendo.stringify(options.models) }); 
    } 
} 

(application/x-www-form-urlencoded) , 단지 그것을 다시 설정하는 모든 contentType 정의를 제거합니다

dataSource = new kendo.data.DataSource({ 
    transport: { 
     read: { 
      url: baseUrl + "/GetAccolades?profileId=" + @profileId, 
      type: "GET", 
      dataType: "json" 
     }, 
     create: { 
      url: baseUrl + "/AddAccolade", 
      type: "POST", 
      dataType: "json" 
     }, 
     update: { 
      url: baseUrl + "/UpdateAccolade", 
      type: "PUT", 
      dataType: "json" 
     }, 
     delete: { 
      url: baseUrl + "/DeleteAccolade", 
      type: "DELETE", 
      dataType: "json" 
     }, 
     parameterMap: function (options, operation) { 
      if (operation !== "read" && options.models) { 
       alert(kendo.stringify(options.models)); 
       return { models: kendo.stringify(options.models) }; 
      } 
     } 
    }, 
    // other DataSource definitions 
}); 

참조 :

kendo.data.DataSource (Kendo UI Documentation)

+0

도움 주셔서 감사합니다."application/json"을 제거하여 오류를 수정했습니다. 그러나 컨트롤러는 검도에서 전달 된 모델을받지 못합니다. 어떤 아이디어? –

+1

해결책을 찾았습니다. https://www.telerik.com/forums/parametermap---no-models-sent-to-controller-001b5bef1c02 –

+0

글쎄, 이미'transport'에'batch = true'가 있습니다. 그래서 두 번째 해결책 ('contentType' 제거하기)이 이제는 효과가있다. 이미 문제가 해결되었거나 여전히 놓친 것이 있습니까? –