剣道グリッドインライン編集CRUDの実装中にエラーが発生しました。新しいレコードを追加して更新すると、エラーコード500が返されます。エラー "無効なJSONプリミティブ:モデル。"コントローラにモデルを渡している間に剣道グリッドのデータソースにアクセスする
私はこの問題がparameterMap
から発生していると考えています。剣道のコントローラーにモデルを渡す正しい方法は何ですか?
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",
を除去することにより
がなくなっています。ただし、コントローラはモデルを取得しません。
これを解決するためのアイデアはありますか?
'alert(kendo.stringify(options.models)) 'からどのような種類のJSON文字列を取得しましたか?通常この問題は、JSON文字列形式ではなくURLエンコードされたパラメータを使用してメソッドを呼び出した後に発生します。 –
見たことがありますか?https://stackoverflow.com/questions/18695302/kendo-datasource-parameter-map – jwatts1980
@Tetsuya Yamamoto形式の整ったJSON文字列があります。 –