添加后显示ID

时间:2018-03-06 14:19:34

标签: angular asp.net-web-api

我想要做的是在添加后立即获取ID 这是我的服务

  public int  Insert<T>(T model)
    {
        var consultation = Mapper.Map<T, Consultation>(model);

        consultationRepository.Insert(consultation);
        return consultation.id;  
    }

插入我在我的服务中取回Id值后,我只想在我的组件中获取该ID,以便我可以在添加后显示它 这是我的组件,

onSubmit(): void {

    swal({
        title: 'Are you sure?',
        text: "You won't be able to revert this!",
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, add it!'
    }).then(() => {
        this.model.DossierId = this.DossierId;
        this.model.DoctorId = this.DoctorId;
        this._consultationservice.post(this.model);
    });

}

任何想法怎么做?并感谢您的帮助

1 个答案:

答案 0 :(得分:0)

你必须在控制器api中使用你的方法

public IHttpActionResult AddConsultation(yourModel consultation)
    {

            try
            {
              int i =  consultationServices.Insert(consultation);
                return Ok(i);

            }
            catch (Exception e)
            {
                return InternalServerError();
            }
    }

和您的consultservice.post(this.model)同样如下:

    postconsultation(model: Consultation): Promise<number>
   {       
    return this.http.post("ApiUrl",model).toPromise()
   .then(response => response).catch(this.handleError);
   }