简体   繁体   English

带有Angular和Kendo UI的ASP.Net MVC

[英]ASP.Net MVC with Angular and Kendo UI

I'm new to ASP, Kendo and programming in general. 我是ASP,Kendo和一般编程的新手。 I'm trying to use Kendo Grid to CRUD the database; 我正在尝试使用Kendo Grid对数据库进行CRUD; but I am kind of lost. 但我有点迷路。

I have a simple class: Person, with Name and Surname. 我有一个简单的类:具有名称和姓氏的人。 The generated Controller and View works just fine, I can CRUD the database. 生成的Controller和View正常工作,我可以对数据库进行CRUD。

Then, using Angular I created this module: 然后,使用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" }                            }
                    }
                }
            })`

The problem is: 问题是:

First, When I create a new person on the Grid, when I press “Create”, I don't know if the data is being passed to the controller. 首先,当我在网格上创建一个新的人时,当我按下“创建”时,我不知道数据是否正在传递给控制器​​。

Second, in the controller, I have no idea of how to receive this information (a json, I believe). 其次,在控制器中,我不知道如何接收此信息(我相信是json)。

Sorry, I am a total beginner. 抱歉,我是一个初学者。

EDIT 编辑

I have this method on the Controller: 我在控制器上有此方法:

[HttpPost]
        public ActionResult Create(Person model)
        {
            if (model != null)
            {
                return Json("Success");
            }
            else
            {
                return Json("An Error Has Occurred");
            }
        }

So, this is being sent to the Person/Create method: { Name: "John", Surname: "Doe"} 因此,这将被发送到Person / Create方法:{名称:“ John”,姓氏:“ Doe”}

Which return Success; 哪个返回成功;

But how do I get these properties from the Json, and populate the model? 但是,如何从Json获取这些属性并填充模型?

Ok so. 好吧

  • POST = create 开机自检=创建
  • GET = well Get in the context of the type of request from browser to server GET = well在浏览器到服务器的请求类型的上下文中获取
  • PUT = Update PUT =更新
  • DELETE = exactly Delete DELETE =完全删除

so those are the verbs associated with HTTP in which RESTFUL APIS reside, for the most part there are lot more to look at, behind the scope of this answer . 因此,这些是与RESTFUL APIS驻留在其中的HTTP相关联的动词在此答案的范围内,大部分情况下还有很多要看的东西。

as a result of the nature of the call it already understands that certain things like the type of data that you sent was in Content-Type: application/json . 由于调用的性质,它已经了解某些事情,例如您发送的数据类型在Content-Type: application/json

The one thing missing from that method which I am surprised it wasn't there was [FromBody] , unless you hand coded the Method yourself rather than scaffolded it. 该方法缺少的一件事是令我惊讶的是[FromBody] ,除非您亲自对方法进行手工编码而不是对其进行脚手架。 Understandable for someone starting out. 对于初学者来说是可以理解的。 :) :)

to see this placing one of those break points next to the if statement will allow you to see if model was indeed populated with the contents of the post call. 看到将这些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");
 }
}

I think that will cover the broad strokes of the question. 我认为这将涵盖问题的广泛范围。 I think you are using an older version of asp.net so JsonResult might be available as well, BadRequest and Created assume ApiController is being used or Controller in Asp.net Core 我认为您使用的是较早版本的asp.net,因此JsonResult也可能可用, BadRequest and Created假定正在使用ApiControllerAsp.net Core Controller

mvermef, thank you very much for all the explanation. mvermef,非常感谢您的所有解释。 I'm using ASP.Net MVC 5, with EF. 我正在使用带有EF的ASP.Net MVC 5。 I did as you told and it work perfectly. 我照你说的做了,效果很好。

I haven't included Web API at first, but needed it to make the [FromBody] work. 起初我没有包括Web API,但需要它来使[FromBody]工作。

Another small but important thing was include contentType on the kendo configuration: create: { url: "/Person/Create", dataType: "json", type: "POST", contentType: "application/json; charset=utf-8" }, 另一个小而重要的事情是在kendo配置上包括contentType:create:{url:“ / Person / Create”,dataType:“ json”,type:“ POST”, contentType:“ application / json; charset = utf-8” },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM