简体   繁体   English

如何使用从Web API Http Put方法返回的Json对象更新Entity?

[英]How to update Entity with Json object returned from a Web API Http Put Method?

My Web API Put method is called properly from my jQuery Ajax but the C# code does not save the update. 我的Web API Put方法已从我的jQuery Ajax中正确调用,但C#代码未保存更新。 The json object does not have all the properties of the MEMBERS Entity. json对象不具有MEMBERS实体的所有属性。 The context does not save any changes to the database. 上下文不会将任何更改保存到数据库。

public void Update(string id, MEMBERS obj)
    {
        var memToUpdate = context.MEMBERS.Find(obj.MEMBERID);
        if (memToUpdate != null)
        {
            context.Entry(memToUpdate).CurrentValues.SetValues(obj);
            int result = context.SaveChanges();
            System.Diagnostics.Debug.WriteLine("save result:" + result);
        }
    }

This code will work but How do I make context update all the properties of the MEMBERS Entity that are in the JSON Object without specifying like this? 该代码将起作用,但是如何使上下文更新JSON对象中MEMBERS实体的所有属性,而无需指定类似内容?

public void Update(string id, MEMBERS obj)
    {
        MEMBERS memToUpdate = context.MEMBERS.Find(obj.MEMBERID);
        if (memToUpdate != null)
        {
            //context.Entry(memToUpdate).CurrentValues.SetValues(obj);
            memToUpdate.FIRSTNAME = obj.FIRSTNAME;
            memToUpdate.LASTNAME = obj.LASTNAME;
            int result = context.SaveChanges();
            System.Diagnostics.Debug.WriteLine("save result:" + result);
        }
    }

Jquery: jQuery:

var data = {
        MEMBERID: "B745",
        FIRSTNAME: "TESTPUT",
        LASTNAME: "UPDATED WEBAPI"
    };

    var json = JSON.stringify(data)

    $.ajax({
        url: 'api/Members/' + data.MEMBERID,
        type: 'PUT',
        contentType: "application/json; charset=utf-8",
        data: json,
        success: function (results) {
            alert("Success");
        }
    })

My question is more about how to do Web API Put partial update. 我的问题更多是关于如何进行Web API局部更新。 I read about the Patch method using Delta<> type from oData. 我从oData中了解了使用Delta <>类型的Patch方法。 I installed Web API oData from nuget but VS complains type or namespace Delta does not exist. 我从nuget安装了Web API oData,但是VS抱怨类型或名称空间Delta不存在。 Has anyone run into this problem? 有没有人遇到这个问题?

It looks like WEB API cannot do partial update unless using oData. 看起来,除非使用oData,否则WEB API无法进行部分更新。 I ended up having to call the GET method to get back the whole object with all properties then changed the value of some of the properties I want and update via PUT method. 我最终不得不调用GET方法以获取具有所有属性的整个对象,然后更改了我想要的某些属性的值并通过PUT方法进行了更新。

$(document).ready(function () {
    $.getJSON('api/Members/B745', function (data) {
        data.FirstName = 'Test Put Success';
        data.LastName = 'My Lastname Is';

        var json = JSON.stringify(data)

        $.ajax({
            url: 'api/Members/' + data.MEMBERID,
            type: 'PUT',
            contentType: "application/json; charset=utf-8",
            data: json,
            success: function (results) {
                alert("Success");
            }
        })

    });
});

You can do partial updates using WebApi without using Odata (you do need to install the libary but the Delta object seems to work fine without the oData stuff). 您可以使用WebApi进行部分更新,而无需使用Odata(您确实需要安装库,但是没有oData的东西,Delta对象似乎可以正常工作)。 I would use PATCH and not PUT as from my understanding PUT is there to replace the whole resource, PATCH is the partial HTTP Verb. 我会使用PATCH而不是PUT,因为据我了解,PUT可以代替整个资源,PATCH是部分HTTP Verb。

You need the odata library (but just for the Delta library, i hope they move this out of here and make it more general as it will be useful) http://www.nuget.org/packages/Microsoft.AspNet.WebApi.OData 您需要odata库(但仅适用于Delta库,我希望他们将其移出此处并使其更通用,因为它将很有用) http://www.nuget.org/packages/Microsoft.AspNet.WebApi。数据

Some code, not sure if this will 100% work, i have a slightly different setup. 一些代码,不确定这是否会100%工作,我的设置略有不同。 I do use something similar to your original code to facilitate my PUT, but only because the Delta.Put method throws loads of errors, which i think is to do with my dates, anyhoo, not related. 我确实使用了与您的原始代码相似的东西来简化我的PUT,但这只是因为Delta.Put方法会引发大量错误,我认为这与我的日期有关,但不相关。

    public void Update(string id, Delta<MEMBERS> obj)
    {
        var memToUpdate = context.MEMBERS.Find(obj.MEMBERID);

        if (memToUpdate != null)
        {
            obj.Patch(memToUpdate);
            int result = context.SaveChanges();
            System.Diagnostics.Debug.WriteLine("save result:" + result);
        }
    }

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

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