public JsonResult Search(PrintingHouseViewModel model) { Response response; try { using (var db = new KiaGalleryContext()) { List <PrintingHouseViewModel> list; var query = db.PrintingHouse.Select(x => x); list = query.Select(item => new PrintingHouseViewModel() { id = item.Id, name = item.Name, phone = item.Phone, phoneNumber = item.PhoneNumber, management = item.Management, active = item.Active, }).ToList(); response = new Response() { status = 200, data = new { list = list }, }; } } catch (Exception ex) { response = Core.GetExceptionResponse(ex); } return(Json(response, JsonRequestBehavior.AllowGet)); }
public JsonResult Save(PrintingHouseViewModel model) { Response response; try { using (var db = new KiaGalleryContext()) { var userId = GetAuthenticatedUserId(); if (model.id != null && model.id > 0) { var printingHouse = db.PrintingHouse.Where(x => x.Id == model.id).SingleOrDefault(); printingHouse.Name = model.name; printingHouse.Code = model.code; printingHouse.PhoneNumber = model.phoneNumber; printingHouse.Phone = model.phone; printingHouse.Management = model.management; printingHouse.Address = model.address; printingHouse.Active = model.active; printingHouse.ModifyUserId = userId; printingHouse.ModifyDate = DateTime.Now; printingHouse.Ip = Request.UserHostAddress; db.SaveChanges(); response = new Response() { status = 200, message = "چاپخانه ویرایش شد", }; } else { var item = new PrintingHouse() { Name = model.name, Code = model.code, PhoneNumber = model.phoneNumber, Phone = model.phone, Management = model.management, Address = model.address, Active = model.active, CreateUserId = userId, ModifyUserId = userId, CreateDate = DateTime.Now, ModifyDate = DateTime.Now, Ip = Request.UserHostAddress, }; db.PrintingHouse.Add(item); db.SaveChanges(); response = new Response() { status = 200, message = "چاپخانه جدید ثبت شد", }; } } } catch (Exception ex) { response = Core.GetExceptionResponse(ex); } return(Json(response, JsonRequestBehavior.AllowGet)); }