![](/img/trans.png)
[英]Performing An Inline Edit With A Kendo Grid Using Angular.js, Kendo UI and ASP.NET MVC
[英]ASP.Net MVC with Angular and Kendo UI
我是ASP,Kendo和一般編程的新手。 我正在嘗試使用Kendo Grid對數據庫進行CRUD; 但我有點迷路。
我有一個簡單的類:具有名稱和姓氏的人。 生成的Controller和View正常工作,我可以對數據庫進行CRUD。
然后,使用Angular創建了這個模塊:
`angular.module("KendoDemos2", ["kendo.directives"])
.controller("MyCtrl2", function ($scope) {
$scope.mainGridOptions = {
dataSource: new kendo.data.DataSource({
transport: {
read: { url: "/Person/Read", dataType: "json" },
create: { url: "/Person/Create", dataType: "json", type: "POST" },
update: { url: "/Person/Update", dataType: "json", type: "PUT" },
destroy: { url: "/Person/Delete", dataType: "json", type: "DELETE" },
parameterMap: function (options, operation) {
if (operation !== "read") {
console.log(options)
return kendo.stringify(options);
}
}
},
batch: false,
pageSize: 10,
schema: {
model: {
id: "Id",
fields: {
Name: { type: "string },
Surname: { type: "string" } }
}
}
})`
問題是:
首先,當我在網格上創建一個新的人時,當我按下“創建”時,我不知道數據是否正在傳遞給控制器。
其次,在控制器中,我不知道如何接收此信息(我相信是json)。
抱歉,我是一個初學者。
編輯
我在控制器上有此方法:
[HttpPost]
public ActionResult Create(Person model)
{
if (model != null)
{
return Json("Success");
}
else
{
return Json("An Error Has Occurred");
}
}
因此,這將被發送到Person / Create方法:{名稱:“ John”,姓氏:“ Doe”}
哪個返回成功;
但是,如何從Json獲取這些屬性並填充模型?
好吧
因此,這些是與RESTFUL APIS駐留在其中的HTTP相關聯的動詞 , 在此答案的范圍內,大部分情況下還有很多要看的東西。
由於調用的性質,它已經了解某些事情,例如您發送的數據類型在Content-Type: application/json
。
該方法缺少的一件事是令我驚訝的是[FromBody]
,除非您親自對方法進行手工編碼而不是對其進行腳手架。 對於初學者來說是可以理解的。 :)
看到將這些break points
之一放在if語句旁邊,將允許您查看是否確實使用post調用的內容填充了model
。
[HttpPost] //because we want to create (new Person(){}) right?
public ActionResult Create([FromBody] Person model){
if(model != null && model.IsValid){
//EntityFramework or what ever DB you are using
_context.Person.Add(model);
_context.SaveChanges();
//doing this returns to the client that it was created as well as the
//Id of the newly inserted record. model.Id was automagically updated with the Id.
return Created("api/person/create", model);
}
else{
//something was horribly wrong with the model sent
return BadRequest("Invalid Model");
}
}
我認為這將涵蓋問題的廣泛范圍。 我認為您使用的是較早版本的asp.net,因此JsonResult
也可能可用, BadRequest and Created
假定正在使用ApiController
或Asp.net Core
Controller
mvermef,非常感謝您的所有解釋。 我正在使用帶有EF的ASP.Net MVC 5。 我照你說的做了,效果很好。
起初我沒有包括Web API,但需要它來使[FromBody]工作。
另一個小而重要的事情是在kendo配置上包括contentType:create:{url:“ / Person / Create”,dataType:“ json”,type:“ POST”, contentType:“ application / json; charset = utf-8” },
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.