示例#1
0
        public ActionResult Create(AppUser model)
        {
            List <string> errors = new List <string>();

            try
            {
                model.Status = (int)DataStatus.Active;
                if (ModelState.IsValid)
                {
                    db.AppUsers.Add(model);
                    db.SaveChanges();
                    return(RedirectToAction("list"));
                }

                foreach (var item in ModelState.Where(x => x.Value.Errors.Any()))
                {
                    errors.Add(item.Value.Errors.FirstOrDefault().ErrorMessage);
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
            }
            if (errors.Count > 0)
            {
                TempData["Errors"] = errors;
            }
            ViewBag.UserRoleId = new SelectList(db.UserRoles, "Id", "Name");
            return(View(model));
        }
        public ActionResult Create(Costumer model)
        {
            List <string> errors = new List <string>();

            try
            {
                model.Status    = (int)DataStatus.Active;
                model.EntryDate = DateTime.Now;
                model.EntryBy   = Utility.Currentuser.Id;


                //db.SaveChanges();
                //return RedirectToAction("index");

                if (ModelState.IsValid)
                {
                    db.Costumers.Add(model);
                    db.SaveChanges();
                    return(RedirectToAction("index"));
                }
                foreach (var item in ModelState.Where(x => x.Value.Errors.Any()))
                {
                    errors.Add(item.Value.Errors.FirstOrDefault().ErrorMessage);
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
            }
            if (errors.Count > 0)
            {
                TempData["Errors"] = errors;
            }
            ViewBag.GenderId = new SelectList(db.Genders, "Id", "Name");


            return(View(model));
        }
        public ActionResult Create(Employee model)
        {
            List <string> errors = new List <string>();

            try
            {
                if (ModelState.IsValid)
                {
                    if (Request.Files.Count > 0)
                    {
                        if (Request.Files[0].ContentLength > 0)
                        {
                            var file = Request.Files[0];
                            if (file != null && file.ContentLength > 0)
                            {
                                WebImage img = new WebImage(Request.Files[0].InputStream);
                                img.Resize(440, 300, true, true);
                                string extension = Path.GetExtension(file.FileName.ToString().ToLower());
                                if (extension == ".jpg" || extension == ".jpeg" || extension == ".png")
                                {
                                    string filename = Guid.NewGuid().ToString() + extension;
                                    if (!Directory.Exists(Server.MapPath("~/Uploads")))
                                    {
                                        Directory.CreateDirectory(Server.MapPath("~/Uploads"));
                                    }
                                    if (!Directory.Exists(Server.MapPath("~/Uploads/Images")))
                                    {
                                        Directory.CreateDirectory(Server.MapPath("~/Uploads/Images"));
                                    }
                                    string physicalPath = Server.MapPath("~/Uploads/Images/" + filename);
                                    img.Save(physicalPath);
                                    model.Photo = string.Format("~/Uploads/Images/{0}", filename);
                                }
                            }
                        }
                    }
                    model.EntryBy   = Utility.Currentuser.Id;
                    model.EntryDate = DateTime.Now;
                    model.Status    = (int)DataStatus.Active;

                    db.Employees.Add(model);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                foreach (var item in ModelState.Where(x => x.Value.Errors.Any()))
                {
                    errors.Add(item.Value.Errors.FirstOrDefault().ErrorMessage);
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
            }
            if (errors.Count > 0)
            {
                TempData["Errors"] = errors;
            }
            ViewBag.GenderId       = new SelectList(db.Genders, "Id", "Name");
            ViewBag.EmployeeTypeid = new SelectList(db.Employeetypes, "Id", "Name");
            return(View());
        }