更新在asp.net中的api控制器调用中不起作用。 [英] Update does not work in api controller call in asp.net .

查看:82
本文介绍了更新在asp.net中的api控制器调用中不起作用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发布我的项目然后将其上传到服务器这里更新不起作用但是当我在visual studio 2012中运行它的工作。



I publish my project then upload it into server here update does not work but when i run it in visual studio 2012 its work.

private bool UpdateShoppingCartItemByCustomerId(int CustId)
        {
            try
            {
                bool Return = false;
                ViewCartHelper objHelper = new ViewCartHelper();
                ViewCartModel model = new ViewCartModel();
                model.GuestCustomerId = _objCustomer.Id;
                model.CustomerId = CustId;
                HttpResponseMessage response = objHelper.Update("api/ViewCart/", model);  // Blocking call!
                if (response.IsSuccessStatusCode)
                {
                    Return = true;
                }
                return Return;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

这是我的控制器电话



控制器也给了以下声音

this is my controller call

controler also given bellow

public bool Put(ViewCartModel model)
        {
            return repository.Update(model);
        }



我的linq代码如下:




My linq code is given bellow

public bool Update(ViewCartModel ProductItem)
        {
            try
            {
                bool bFlag = false;
                if (ProductItem == null)
                {
                    throw new ArgumentNullException("item");
                }
                //string dtDate = System.DateTime.Now.ToString();

                using (var contextDb = new AllVetMedDbEntities())
                {
                    var query = (from Item in contextDb.ShoppingCartItems
                                 where Item.Id == ProductItem.Id
                                 select Item).SingleOrDefault();

                    var original = contextDb.ShoppingCartItems.Find(ProductItem.Id);
                    if (ProductItem.GuestCustomerId > 0)
                    {
                        var SoppingCartList = (from objcartItem in contextDb.ShoppingCartItems
                                               where objcartItem.CustomerId == ProductItem.GuestCustomerId
                                               select objcartItem).ToList();
                        SoppingCartList.ForEach(x => x.CustomerId = ProductItem.CustomerId);

                        contextDb.SaveChanges();
                        bFlag = true;

                        contextDb.SaveChanges();
                        bFlag = true;

                    }
                    else
                    {
                        if (original != null)
                        {
                            original.Quantity = ProductItem.Quantity;
                            contextDb.SaveChanges();
                            bFlag = true;
                        }
                    }
                    //}
                }


                return bFlag;
            }
            catch(Exception ex) {
                throw ex;
            }
        }

推荐答案

this problem has been solve by adding this on web.config file.
<modules runallmanagedmodulesforallrequests="true">
        <remove name="WebDAVModule"> **&lt;- add this**
    </remove></modules>


确保数据库中的命名约定与se上的命名约定完全相同rver。我可能错了,但查看你的代码我唯一可以看到更新错误的地方是

Make sure your naming conventions in your db is exactly the same as on the server. I might be wrong but looking over your code the only place I can see that it will update wrong is
using (var contextDb = new AllVetMedDbEntities())
                {





可能是数据没有在数据库中更新,因为它不一样或者不符合你电脑上的相同命名(在调试中) )



_________________________EDIT______________________________________________



另外只看一看linq中的代码有重复的



It might be that the data is not updating in the db as it is not the same or does not comply with the same naming as on your pc (in debug)

_________________________EDIT______________________________________________

Also just have a look at the code in linq there is a duplicate

contextDb.SaveChanges();
bFlag = true;

contextDb.SaveChanges();
bFlag = true;





你也永远不会使用linq查询...



You also never use the linq query...

var query = (from Item in contextDb.ShoppingCartItems
             where Item.Id == ProductItem.Id
             select Item).SingleOrDefault();





但是我确实认为你可能打算这样做...



But I do gather that you probably meant to do this...

var query = (from Item in contextDb.ShoppingCartItems
             where Item.Id == ProductItem.Id
             select Item).SingleOrDefault();

             var original = contextDb.ShoppingCartItems.Find(query.Id);


这篇关于更新在asp.net中的api控制器调用中不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆