ASP.NET MVC 5 WEB API PUT请求

时间:2021-11-13 06:19:32

Jquery请求,使用JSON.stringify进行序列化,并且必须要指定传输格式contentType

$.ajax({ url: ‘/api/sysUser?token=‘+token, type: ‘PUT‘, data: JSON.stringify({ "userID":id, "userName": $(‘#inputUserName‘).val(), "cellphone": $(‘#inputCellPhone‘).val(), "email": $(‘#inputEmail‘).val(), "telephone": $(‘#inputTelePhone‘).val(), "showName": $(‘#inputShowName‘).val(), "roleID": $(‘#selectRole‘).val() }), contentType: ‘application/json‘, dataType: "json", success: function (result) { var re = eval(‘(‘ + result + ‘)‘); alert(re.message); $("#myModal").modal("hide"); oTable.fnDraw(); }, error: function (err) { alert("error"); } });

后台代码,使用FromBody获取参数数据

[HttpPatch] [HttpPut] public common.HttpResult update([FromBody]models.sys_user sysUser) { common.HttpResult result = new common.HttpResult(); models.sys_user user = new models.sys_user(); user.userID = sysUser.userID; user.userName = sysUser.userName; user.cellphone = sysUser.cellphone; user.email = sysUser.email; user.roleID = sysUser.roleID; user.showName = sysUser.showName; user.telephone = sysUser.telephone; bool re = bll.sysUserManager.Update(user); if (re) { result.state = common.ResultType.success.ToString(); result.message = lang.message.UPDATESUCCES; } else { result.state = common.ResultType.error.ToString(); result.message = lang.message.UPDATEFAIL; } return result; } #endregion

参考博客:ASP.NET MVC学习系列(二)-WebAPI请求

ASP.NET MVC 5 WEB API PUT请求