使用AngularJS的$ http.put方法 [英] Using AngularJS's $http.put method

查看:160
本文介绍了使用AngularJS的$ http.put方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用AngularJS更新使用$ http.put方法的一些数据。不过,我已经试过几个变化上调用此方法,并屡次未能调用put方法在我的控制器 - 或者,如果这样做,它会创建一个新的实体,而不是更新旧数据。这里是我的code:

(我知道我的API的其余部分的工作原理后,让汽车做工精细)

CarController.cs

 公共无效将(INT CarId,[FromBody]租车C)
    {
        System.Diagnostics.Debug.WriteLine(CarController.Put()调用);
        c.CarId = CarId;
        如果(!_repo.UpdateCar(三)||!_repo.Save())
        {
            抛出新的Htt presponseException(的HTTPStatus code.NotFound);
        }
    }

信息库(_repo):

 公共BOOL UpdateCar(车车)
    {
        如果(汽车== NULL)
        {
            抛出新的ArgumentNullException(车);
        }
        。INT指数= _ctx.Cars.ToList()FindIndex(C => c.CarId == car.CarId);
        如果(指数== -1)
        {
            返回false;
        }
        。_ctx.Cars.ToList()RemoveAt移除(指数);
        _ctx.Cars.Add(车);
        返回true;    }

AngularJS脚本:

  VAR _linkPersonAndCar =功能(人,车){
    变种推迟= $ q.defer();    警报(_ linkPersonAndCar()\\ nPerson =+ JSON.stringify(人)+\\ NCAR =+ JSON.stringify(汽车));    //更新汽车
    car.persons.splice(0,0,人);    警报(_ linkPersonAndCar()试图把);    $ http.put(/ API /汽车/+ car.carId,汽车)
        。然后(函数()
        {
            警报(_ linkPersonAndCar() - 成功!);
            deferred.resolve();
        },
        功能()
        {
            警报(_ linkPersonAndCar() - 更新故障车);
            deferred.reject();
        });    警报(_ linkPersonAndCar() - 完成);    返回deferred.promise;
};


解决方案

好了,看我的意见就分手这成单独的问题提供咨询意见。但我看到至少有一个问题,您的EF code:

  _ctx.Cars.ToList()RemoveAt移除(指数)。

调用了ToList()创建一个从供应商(EF上下文)断开的名单,并创建一个在内存中的列表。然后,从该列表,这将不成事,因为你不使用任何后续的code的列表中删除的项目。这不会从数据库中,我认为这是你的意图删除的实体。在任何情况下,你要更新现有的汽车,而不是删除并重新添加它(至少这是我的假设)。

  _ctx.Cars.Add(车);

这会随时添加一个新的汽车。

您想要的东西更像是:

  VAR ctxCar = _ctx.Cars.SingleOrDefault(C => c.CarId = car.CarId);如果(ctxCar == NULL)
{
  返回false;
}//这里你想从你的参数属性映射到上下文的车
ctxCar.Property1 = car.Property1;
//等等。返回true;

I am attempting to use AngularJS to update some data using the $http.put method. However, I have tried several variations on calling this method and it has repeatedly failed to call the put method in my controller - or, if it does, it creates a new entity rather than updating the old data. Here is my code:

(I know the rest of my api works as post and get work fine for cars)

CarController.cs

    public void Put(int CarId, [FromBody] Car c)
    {
        System.Diagnostics.Debug.WriteLine("CarController.Put() Called");
        c.CarId = CarId;
        if (!_repo.UpdateCar(c) || !_repo.Save())
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
    }

Repository ("_repo"):

    public bool UpdateCar(Car car)
    {
        if (car == null)
        {
            throw new ArgumentNullException("car");
        }
        int index = _ctx.Cars.ToList().FindIndex(c => c.CarId == car.CarId);
        if (index == -1)
        {
            return false;
        }
        _ctx.Cars.ToList().RemoveAt(index);
        _ctx.Cars.Add(car);
        return true;

    }

AngularJS script:

var _linkPersonAndCar = function(person, car) {
    var deferred = $q.defer();

    alert("_linkPersonAndCar()\nPerson = " + JSON.stringify(person) + "\nCar = " + JSON.stringify(car));

    //Update car
    car.persons.splice(0, 0, person);

    alert("_linkPersonAndCar() attempting put");

    $http.put("/api/cars/"+car.carId, car)
        .then(function ()
        {
            alert("_linkPersonAndCar() - Success!");
            deferred.resolve();
        },
        function ()
        {
            alert("_linkPersonAndCar() - Failure updating car");
            deferred.reject();
        });

    alert("_linkPersonAndCar() - Complete");

    return deferred.promise;
};

解决方案

Well, see my comments for advice on breaking up this into separate questions. But I see at least one problem with your EF code:

_ctx.Cars.ToList().RemoveAt(index);

Calling ToList() creates a list disconnected from the provider (EF Context), and creates an in-memory list. You then remove the item from that list, which will not accomplish anything, since you don't use that list in any subsequent code. This will not delete the entity from the database, which I think was your intent. In any case, you want to update the existing car, not delete and re-add it (at least that's my assumption).

_ctx.Cars.Add(car);

This will always add a new car.

You want something more like:

var ctxCar = _ctx.Cars.SingleOrDefault(c => c.CarId = car.CarId);

if (ctxCar == null)
{
  return false;
}

//here you want map properties from your parameter to your context's car
ctxCar.Property1 = car.Property1;
//etc.

return true;

这篇关于使用AngularJS的$ http.put方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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