Angular+C#项目API调用时FK参数未定义问题求助
问题:Angular调用API时FK_Company和FK_Activity始终为undefined
项目目标:每次调用API时修改CompanyActivity实体中的ShowInList布尔值
技术栈:后端C# + SQL,前端Angular 17
后端代码
API接口代码
[HttpPost, Route("/api/[controller]/ChangeShowInListStatus/")] public async Task<IActionResult> ChangeShowInListStatus([FromBody]CompanyActivity companyActivity, int FK_Company, int FK_Activity, DateTime InsertDate) { _companyActivityService.UpdateCompanyActivity(companyActivity, FK_Company, FK_Activity, InsertDate); return Ok(); }
UpdateCompanyActivity方法
public void UpdateCompanyActivity(CompanyActivity companyActivity, int FK_Company, int FK_Activity, DateTime InsertDate) { GetBy(a => a.FK_Activity == FK_Activity && a.FK_Company == FK_Company && a.InsertDate == InsertDate).FirstOrDefault(); companyActivity.ShowInList = !companyActivity.ShowInList; UpdateEntityProperties(companyActivity, true, new string[] { "ShowInList" }); }
UpdateEntityProperties方法
public virtual int UpdateEntityProperties(TEntity entity, bool saveChanges, params string[] propertiesToUpdate) { UpdateValidation(entity, propertiesToUpdate); _repository.UpdateEntityProperties(entity, propertiesToUpdate); if (saveChanges) { _uow.SaveChanges(); } return entity.ID; }
CompanyActivityDTO
using iTextSharp.text; using System; using System.Collections.Generic; namespace H4Pro.Web.Mappings.ModelsDTO.Productivity { public class CompanyActivityDTO { public int ID { get; set; } public int FK_Company { get; set; } public int FK_Activity { get; set; } public DateTime InsertDate { get; set; } } }
前端代码
点击触发函数
changeShowInListStatus(FK_Company: number, FK_Activity: number) { this.getActivitiesByCompany(); this.FK_Company = this.currentUser.FK_Company; this.FK_Activity = this.activity.ID; this.companyActivityService.changeShowInListStatus(FK_Company, FK_Activity).subscribe(() => {}); }
Service调用代码
changeShowInListStatus(FK_Company: number, FK_Activity: number) { return this.http.get<ActivityDTO>( environment.apiFullUrl + "/Activity/ChangeShowInListStatus/" + FK_Company + "/" + FK_Activity ); }
当前问题
点击触发函数时,传入的FK_Company和FK_Activity始终为undefined,无法正常调用API修改ShowInList值。
排查与解决方案
前端参数传递与使用问题
- 检查模板点击事件绑定:确认触发
changeShowInListStatus时,是否正确传入了FK_Company和FK_Activity的实际值,比如模板中是否写为(click)="changeShowInListStatus(当前公司ID, 当前活动ID)",确保绑定的变量在组件中已正确赋值。 - 当前函数内重新赋值了组件属性
this.FK_Company和this.FK_Activity,但调用服务时仍使用了参数传入的可能未定义的值。可直接使用组件内已有的可靠值调用服务:changeShowInListStatus() { this.getActivitiesByCompany(); const companyId = this.currentUser?.FK_Company; const activityId = this.activity?.ID; if (companyId && activityId) { this.companyActivityService.changeShowInListStatus(companyId, activityId).subscribe(() => {}); } else { console.error("公司ID或活动ID未定义"); } }
- 检查模板点击事件绑定:确认触发
前后端请求方式不匹配
- 后端API定义为
HttpPost,但前端使用http.get请求,参数传递逻辑不兼容。同时后端接口混合了FromBody和URL参数,容易导致参数接收失败。 - 修正方案:
- 后端调整路由与参数接收:
[HttpPost, Route("/api/[controller]/ChangeShowInListStatus/{FK_Company}/{FK_Activity}")] public async Task<IActionResult> ChangeShowInListStatus(int FK_Company, int FK_Activity, [FromBody]DateTime InsertDate) { var companyActivity = _companyActivityService.GetBy(a => a.FK_Activity == FK_Activity && a.FK_Company == FK_Company && a.InsertDate == InsertDate).FirstOrDefault(); if (companyActivity == null) return NotFound(); companyActivity.ShowInList = !companyActivity.ShowInList; _companyActivityService.UpdateEntityProperties(companyActivity, true, new string[] { "ShowInList" }); return Ok(); } - 前端改为Post请求:
changeShowInListStatus(FK_Company: number, FK_Activity: number) { return this.http.post( `${environment.apiFullUrl}/Activity/ChangeShowInListStatus/${FK_Company}/${FK_Activity}`, new Date() // 传递InsertDate,根据实际需求调整 ); }
- 后端调整路由与参数接收:
- 后端API定义为
后端实体更新逻辑错误
- 当前
UpdateCompanyActivity方法中,查询到的数据库实体未赋值给变量,后续修改的是前端传入的对象而非数据库中存在的实体,会导致更新无效。修正为:public void UpdateCompanyActivity(int FK_Company, int FK_Activity, DateTime InsertDate) { var companyActivity = GetBy(a => a.FK_Activity == FK_Activity && a.FK_Company == FK_Company && a.InsertDate == InsertDate).FirstOrDefault(); if (companyActivity == null) throw new ArgumentException("未找到对应的CompanyActivity"); companyActivity.ShowInList = !companyActivity.ShowInList; UpdateEntityProperties(companyActivity, true, new string[] { "ShowInList" }); }
- 当前
内容的提问来源于stack exchange,提问作者Jany
相关产品推荐
相关产品推荐

