![](/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.