使用Dictionary接收前端數(shù)據(jù),并用反射更新對像數(shù)據(jù)
前端調(diào)用方式
?wx.request({
????????????url:?app.globalData.serverHttpUrl?+?"/JoinTeam/UpdateLeader",
????????????data:?{?TeamId:?thisProxy.data.theTeam.TeamId,?PlayerId:?e.currentTarget.id?},
????????????method:?'PUT',
????????????header:?{?'Cookie':?wx.getStorageSync('wxloginToken'),'content-type':?'application/x-www-form-urlencoded'?},
????????????success:?(res)?=>?{
????????????????console.log(res)
????????????}
})
控制器
? ? ? ? /// <summary>
? ? ? ? /// 變更球隊(duì)相關(guān)信息
? ? ? ? /// </summary>
? ? ? ? /// <param name="teamNewInfo"></param>
? ? ? ? /// <param name="uploadFiles"></param>
? ? ? ? /// <returns></returns>
? ? ? ? [HttpPut("Update")]
? ? ? ? public async Task<JsonResult> PutAsync([FromForm] Dictionary<string, string> DictKeyValue, [FromForm] IFormCollection uploadFiles)
? ? ? ? {
? ? ? ? ? ? return new JsonResult(await _DataTeamRep.UpdateRecInfo(DictKeyValue));
? ? ? ? }
數(shù)據(jù)操作? ?
? ? ? ? /// <summary>
? ? ? ? /// 更新記錄信息
? ? ? ? /// </summary>
? ? ? ? /// <param name="RecNewInfo">新的球隊(duì)信息</param>
? ? ? ? /// <param name="tempLogos">新的球隊(duì)Logo文件</param>
? ? ? ? /// <returns></returns>
? ? ? ? public async Task<VMResult<MTeam>> UpdateRecInfo(Dictionary<string, string> DictKeyValue)
? ? ? ? {
? ? ? ? ? ? if (DictKeyValue==null || !DictKeyValue.Keys.Contains("Id"))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return new VMResult<MTeam>() { ResultCode = -1, ResultMsg = "S:參數(shù)不正確",ResultEntity=null };
? ? ? ? ? ? }
? ? ? ? ? ? //查詢需要修改的球隊(duì)記錄。
? ? ? ? ? ? MTeam WillUpdateRec = await _DbContext.tb_team.FindAsync(DictKeyValue["Id"]);
? ? ? ? ? ? if (WillUpdateRec == null)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return new VMResult<MTeam>() { ResultCode = -2, ResultMsg = "S:未找到球隊(duì)", ResultEntity = null };
? ? ? ? ? ? }
? ? ? ? ? ? //更新需要更新的字段
? ? ? ? ? ? List<string> NoUpdateFields = new() { "Id", "CreateTime" };
? ? ? ? ? ? var RecNewInfoProperties = WillUpdateRec.GetType().GetProperties();
? ? ? ? ? ? foreach (var item in RecNewInfoProperties.Where(x => !NoUpdateFields.Contains(x.Name)).ToList())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if(DictKeyValue.Keys.Contains( item.Name))
? ? ? ? ? ? ? ? {
?switch (item.PropertyType.Name)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? case "Int32":
? ? ? ? ? ? ? ? ? ? ? ? ? ? item.SetValue(WillUpdateRec, Int32.Parse(DictKeyValue[item.Name]));
? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? case "String":
? ? ? ? ? ? ? ? ? ? ? ? ? ? item.SetValue(WillUpdateRec, DictKeyValue[item.Name]);
? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? if (await _DbContext.SaveChangesAsync() > 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return new VMResult<MTeam>() { ResultCode = -2, ResultMsg = "S:更新成功", ResultEntity = WillUpdateRec };
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return new VMResult<MTeam>() { ResultCode = -3, ResultMsg = "S:未更新信息", ResultEntity = null };
? ? ? ? ? ? }
? ? ? ? }
? ? }
}