• OData V4 系列 Ajax请求 CRUD


           OData 学习目录

           上一篇已经完成了服务创建,本篇主要介绍如何通过Ajax请求Odata服务,OData操作主要有 Get、Post、Patch、Put、Delete等操作。

        Post 操作       

            public async Task<IHttpActionResult> Post(Product product)
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }
                _dbContext.Products.Add(product);
                await _dbContext.SaveChangesAsync();
                return Created(product);
            }
    function addPro() {
            var pro = {
                Name: "OData Create",
                Price: 1024,
                Category: "IT"
            };
            //创建产品信息
            $.ajax({
                url: "/Odata/Products",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataType: "JSON",
                data: JSON.stringify(pro),
                success: function (r) {
                    alert(r.Id);
                },
                error: function (e) {
                    debugger;
                }
            });
        }

             Patch 操作 只修改有变化的实体属性

    public async Task<IHttpActionResult> Patch([FromODataUri] int key, Delta<Product> product)
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }
                var entity = await _dbContext.Products.FindAsync(key);
                if (entity == null)
                {
                    return NotFound();
                }
                product.Patch(entity);
                try
                {
                    await _dbContext.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }
                return Updated(entity);
            }
    var pro = {
                SupplierId: 2
            };
    
            //创建产品信息
            $.ajax({
                url: "/Odata/Products(" + id + ")",
                type: "Patch",
                contentType: "application/json; charset=utf-8",
                dataType: "JSON",
                data: JSON.stringify(pro),
                success: function (r) {
                    debugger;
                },
                error: function (e) {
                    debugger;
                }
            });

              Update 操作,修改所有实体

    public async Task<IHttpActionResult> Put([FromODataUri] int key, Product update)
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }
                if (key != update.Id)
                {
                    return BadRequest();
                }
                _dbContext.Entry(update).State = EntityState.Modified;
                try
                {
                    await _dbContext.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!Exists(key))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return Updated(update);
            }

               Delete 操作

    public async Task<IHttpActionResult> Delete([FromODataUri] int key)
            {
                var product = await _dbContext.Products.FindAsync(key);
                if (product == null)
                {
                    return NotFound();
                }
                _dbContext.Products.Remove(product);
                await _dbContext.SaveChangesAsync();
                return StatusCode(HttpStatusCode.NoContent);
            }
    function deletePro(id) {
            $.ajax({
                url: "/Odata/Products(" + id + ")",
                contentType: "application/json; charset=utf-8",
                type: "Delete",
                success: function (r) {
                    debugger;
                },
                error: function (r) {
                    debugger;
                }
            });
        }
  • 相关阅读:
    MySQL存储引擎MyISAM和InnoDB有哪些区别?
    python发起post请求获取json数据使用requests方法
    和 Python 2.x 说再见!项目移到python3
    php memcache 缓存与memcached 客户端的详细步骤
    Ubuntu16.04安装Nginx+PHP5.6+MySQL5.6
    element-ui select 下拉框 实现分页 通过css样式
    技术_pm发展历程
    前端_git用法
    前端_javascript本地实现分页(摘录)
    生活_人生感悟
  • 原文地址:https://www.cnblogs.com/Fengger/p/4958728.html
Copyright © 2020-2023  润新知