Api查询
public bool ApiUpdate(int id,UpdateCustomer model)
{
string msg;
ICustomerBll bll = new CustomerBll();
MODEL.Customer cmodel = bll.Search(id);
cmodel.NickName = model.NickName;
cmodel.HeadImg = model.HeadImg;
bool isOK= bll.Update(cmodel, out msg);
return isOK;
}
控制器
[HttpPut]
[Route("api/User/UpdateCustomer")]public object UpdateCustomer()
{
//获取headers里面的值
int uid = Convert.ToInt32(Request.Headers.GetValues("uid").ToList()[0]);
User u = bll.Search(uid);
int ID = Convert.ToInt32(u.CustomerID);
ViewModel.UpdateCustomer model = new ViewModel.UpdateCustomer();
HttpPostedFile imgFile = HttpContext.Current.Request.Files["HeadImg"];
string NickName =HttpContext.Current.Request["NickName"];
//生成新图片名 Guid.NewGuid():避免重复性
string guid = System.Guid.NewGuid().ToString();
string newName = guid + imgFile.FileName;
string path =HttpContext.Current.Server.MapPath("~/HeadImg/");
if (imgFile != null)
{
//保存图片
imgFile.SaveAs(path + newName);
model.HeadImg =newName;
}
else
{
return "请上传头像";
}
List<MODEL.Customer> list = cbll.Search(x => x.NickName == NickName);
if (list.Count == 0)
{
model.NickName = NickName;
}
else
{
return "用户名已存在";
}
bool isOK = false;
isOK = cbll.ApiUpdate(ID, model);
return Json(new { state = isOK });
}