使用put方法Web API的多个更新数据

时间:2018-07-15 10:29:27

标签: asp.net-web-api

如何使用put方法Web API多次更新数据。我使用了单次更新,但无法编写多次更新的代码。我正在使用json通过邮递员应用程序更新数据。单个更新通过它正常工作,但我不知道如何进行多次更新。请帮助

 public HttpResponseMessage Put(DelegateTable delegatestable)
        {
            try
            {

                using (var ctx = new ShowContext())
                {

                    var delegatedata = ctx.delegates.FirstOrDefault(s => s.Barcode__c == delegatestable.Barcode__c);

                    if (delegatedata != null)
                    {
                        if (delegatestable.Salutation__c!=null)
                        {
                            delegatedata.Salutation__c = delegatestable.Salutation__c;
                        }
                        if (delegatestable.First_Name__c != null)
                        {
                            delegatedata.First_Name__c = delegatestable.First_Name__c;
                        }
                        if (delegatestable.Last_Name__c != null)
                        {
                            delegatedata.Last_Name__c = delegatestable.Last_Name__c;
                        }
                        if (delegatestable.Account_Name__c != null)
                        {
                            delegatedata.Account_Name__c = delegatestable.Account_Name__c;
                        }
                        if (delegatestable.Contact_Email__c != null)
                        {
                            delegatedata.Contact_Email__c = delegatestable.Contact_Email__c;
                        }
                        if (delegatestable.Category__c != null)
                        {
                            delegatedata.Category__c = delegatestable.Category__c;
                        }
                        if (delegatestable.Conference_Type__c != null)
                        {
                            delegatedata.Conference_Type__c = delegatestable.Conference_Type__c;
                        }
                        if (delegatestable.Conference_Selection__c != null)
                        {
                            delegatedata.Conference_Selection__c = delegatestable.Conference_Selection__c;
                        }
                        if (delegatestable.Payment_Status_Interface__c != null)
                        {
                            delegatedata.Payment_Status_Interface__c = delegatestable.Payment_Status_Interface__c;
                        }

                        //  delegatedata.Barcode__c = delegatestable.Barcode__c;

                        ctx.SaveChanges();
                        return Request.CreateResponse(HttpStatusCode.OK, "Record updated");
                    }
                    else
                    {
                        return Request.CreateResponse(HttpStatusCode.NoContent, delegatedata);
                    }
                }
            }
            catch (Exception ex)
            {

                return Request.CreateResponse(HttpStatusCode.BadGateway, ex);
            }
        }

尝试以下代码进行多次更新,但如何在邮递员应用程序中为其编写json格式以对其进行测试。请帮助

  public HttpResponseMessage Put(List<DelegateTable> delegatestable)
        {
            try
            {

                using (var ctx = new ShowContext())
                {

                    foreach (DelegateTable item in delegatestable)
                    {
                        //DelegateTable _ObjdelegateTable = new DelegateTable();
                        var delegatedata = ctx.delegates.FirstOrDefault(s => s.Barcode__c == item.Barcode__c);

                        if (delegatedata != null)
                        {
                            if (item.Salutation__c != null)
                            {
                                delegatedata.Salutation__c = item.Salutation__c;
                            }
                            if (item.First_Name__c != null)
                            {
                                delegatedata.First_Name__c = item.First_Name__c;
                            }
                            if (item.Last_Name__c != null)
                            {
                                delegatedata.Last_Name__c = item.Last_Name__c;
                            }
                            if (item.Account_Name__c != null)
                            {
                                delegatedata.Account_Name__c = item.Account_Name__c;
                            }
                            if (item.Contact_Email__c != null)
                            {
                                delegatedata.Contact_Email__c = item.Contact_Email__c;
                            }
                            if (item.Category__c != null)
                            {
                                delegatedata.Category__c = item.Category__c;
                            }
                            if (item.Conference_Type__c != null)
                            {
                                delegatedata.Conference_Type__c = item.Conference_Type__c;
                            }
                            if (item.Conference_Selection__c != null)
                            {
                                delegatedata.Conference_Selection__c = item.Conference_Selection__c;
                            }
                            if (item.Payment_Status_Interface__c != null)
                            {
                                delegatedata.Payment_Status_Interface__c = item.Payment_Status_Interface__c;
                            }

                            //  delegatedata.Barcode__c = delegatestable.Barcode__c;

                            ctx.SaveChanges();
                            return Request.CreateResponse(HttpStatusCode.OK, "Record updated");
                        }
                        else
                        {
                            return Request.CreateResponse(HttpStatusCode.NoContent, delegatedata);
                        }
                    }
                    var message = Request.CreateResponse(HttpStatusCode.Created, delegatestable);
                    message.Headers.Location = new Uri(Request.RequestUri.ToString());
                    return message;
                }
            }
            catch (Exception ex)
            {

                return Request.CreateResponse(HttpStatusCode.BadGateway, ex);
            }
        }

0 个答案:

没有答案