以保存用户为例:
api接口:
[HttpPost] public JObject SaveUser([FromBody]T_Sys_User model) { TaskResult result = new UserManageBusiness().SaveUser(model); return JObject.FromObject(result); } [HttpPost] public JObject DeleteUser([FromBody]dynamic json) { string Id = json.Id; TaskResult result = new UserManageBusiness().DeleteUserById(Id); return JObject.FromObject(result); }
调用JS:
///将表单元素转换为json对象 function formToObject(objId) { var o = {}; var a = $("#"+objId).serializeArray(); $.each(a, function () { if (o[this.name]) { if (!o[this.name].push) { o[this.name] = [o[this.name]]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); return o; }
function saveUser() { var formObj = formToObject("UserForm"); if (formObj.role != null) { if (typeof formObj.role == "string") { formObj.role = [formObj.role]; } } var url = userDetail.Config.urls.saveUrl; $.ajax({ type: "Post", url: url, data: JSON.stringify(formObj), contentType: "application/json; charset=utf-8", success: function (data) { if (data.IsSuccess) { BootstrapDialog.alert('保存成功!', function () { window.location.href = userDetail.Config.urls.backUrl; }); } else { BootstrapDialog.danger(data.Message); } }, error: function (err) { BootstrapDialog.danger(err.responseText); } }); }
function del() { var selectedArray = $('#tb_User').bootstrapTable("getAllSelections"); if (selectedArray.length == 0) { BootstrapDialog.alert('请先勾选一条记录'); return false; } var selectedItems = []; for (var i = 0; i < selectedArray.length; i++) { selectedItems.push(selectedArray[i].UserId); } BootstrapDialog.confirm("确定要删除吗?", function (result) { if (result) { var url = "/api/JackService/DeleteUser"; $.ajax({ type: "Post", url: url, data: JSON.stringify({ Id: selectedItems.join(",") }), contentType: "application/json", success: function (data) { if (data.IsSuccess) { BootstrapDialog.alert('删除成功!', function () { location.reload(); }); } else { BootstrapDialog.danger(data.Message); } }, error: function (err) { BootstrapDialog.danger(err.responseText); } }); } }); }