带有Angular和Kendo UI的ASP.Net MVC

带有Angular和Kendo UI的ASP.Net MVC

问题描述:

我是ASP,Kendo和一般编程的新手。 我想用Kendo Grid来删除数据库;但我有点失落。带有Angular和Kendo UI的ASP.Net MVC

我有一个简单的类:Person,名称和姓氏。 生成的控制器和视图工作得很好,我可以CRUD数据库。

然后,采用了棱角分明我创造了这个模块:

`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"); 
      } 
     } 

所以,这是被发送到人/制作方法: {名称: “约翰”,姓“ Doe“}

哪个返回成功;

但是,如何从Json中获取这些属性并填充模型?

+0

'什么编辑您使用的?'欢迎到左右为好:) – mvermef

+0

嘿,谢谢! :) 我在Visual Studio的2015年 – Migu3litto

+0

你想用个破发点,看看数据如何到处移动到不同的电话..在编辑器屏幕左侧的代码块你的缘近左侧边缘附近的工作中,你可以切换不同的东西,比如书签,但在次重要的是破发点允许执行代码在颜色'pause'它的红色,并允许“调试” @那个位置,你可以看到刚才的一切与该代码相关。 – mvermef

好吧。

  • POST =创建
  • GET =好在请求的类型的场景中获得从 浏览器到服务器
  • PUT =更新
  • DELETE =正好删除

所以那些是动词与HTTP相关的w HEST RESTFUL APIS居住,大部分还有很多要看,这个回答的范围

作为调用性质的结果,它已经理解某些事情,例如您发送的数据类型是Content-Type: application/json

从这种方法中遗漏的一件事我很惊讶它不存在 [FromBody],除非您自己手工编码Method而不是脚手架它。对于刚开始的人可以理解。 :)

看到这将break points其中之一放在旁边的if语句将允许您看看model是否确实填充了post调用的内容。

[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承担ApiControllerAsp.net Core

mvermef正在使用或Controller,非常感谢你对所有的解释。 我正在使用ASP.Net MVC 5和EF。 我照你所说的完成了工作。

我没有将网络API在第一,但它需要使[FromBody]工作。

另一个小但重要的是包括在剑道配置的contentType: 创建:{URL: “/人/创建”,数据类型: “JSON”,键入: “POST”,的contentType:“应用/ JSON的;字符集= UTF-8"},

+0

很高兴它为你工作:) – mvermef