Ejemplo n.º 1
0
        public async Task <ActionResult> Edit(User user)
        {
            if (ModelState.IsValid)
            {
                db.Entry(user).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            ViewBag.UserTypeId = new SelectList(ComboHelper.GetUserTypeId(), "UserTypeId", "Description", user.UserTypeId);
            return(View(user));
        }
Ejemplo n.º 2
0
        // GET: Users/Edit/5
        public async Task <ActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            User user = await db.Users.FindAsync(id);

            if (user == null)
            {
                return(HttpNotFound());
            }
            ViewBag.UserTypeId = new SelectList(ComboHelper.GetUserTypeId(), "UserTypeId", "Description", user.UserTypeId);
            return(View(user));
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> Create(UserView view)
        {
            if (ModelState.IsValid)
            {
                var user = this.ToUsers(view);
                using (var transaction = db.Database.BeginTransaction())
                {
                    db.Users.Add(user);
                    try
                    {
                        await db.SaveChangesAsync();


                        UsersHelper.CreateUserASP(view.UserName, RolesHelper.PowerUser, view.Passwords);

                        var pic    = string.Empty;
                        var folder = "~/Content/Users";

                        if (view.LifeLogo != null)
                        {
                            pic = FilesHelper.UploadPhoto(view.LifeLogo, folder, string.Format("{0}", user.FirstName), string.Format("{0}", user.UserId));

                            user.ImageMimeType = view.LifeLogo.ContentType;
                            int    length = view.LifeLogo.ContentLength;
                            byte[] buffer = new byte[length];
                            view.LifeLogo.InputStream.Read(buffer, 0, length);
                            user.ImagenUser = buffer;
                        }

                        if (!string.IsNullOrEmpty(pic))
                        {
                            user.ImagePath       = pic;
                            db.Entry(user).State = EntityState.Modified;
                            await db.SaveChangesAsync();
                        }

                        transaction.Commit();

                        return(RedirectToAction("Index"));
                    }
                    catch (System.Exception ex)
                    {
                        transaction.Rollback();
                        if (ex.InnerException != null &&
                            ex.InnerException.InnerException != null &&
                            ex.InnerException.InnerException.Message.Contains("_Index") &&
                            ex.InnerException.InnerException.Message.Contains("REFERENCE"))
                        {
                            ModelState.AddModelError(string.Empty, "There are a record with the same value");
                        }
                        else
                        {
                            ModelState.AddModelError(string.Empty, ex.Message);

                            string message = string.Format("<b>Message:</b> {0}<br /><br />", ex.Message);
                            message += string.Format("<b>StackTrace:</b> {0}<br /><br />", ex.StackTrace.Replace(Environment.NewLine, string.Empty));
                            message += string.Format("<b>Source:</b> {0}<br /><br />", ex.Source.Replace(Environment.NewLine, string.Empty));
                            message += string.Format("<b>TargetSite:</b> {0}", ex.TargetSite.ToString().Replace(Environment.NewLine, string.Empty));
                            ModelState.AddModelError(string.Empty, message);
                        }
                    }
                }
            }

            ViewBag.UserTypeId = new SelectList(ComboHelper.GetUserTypeId(), "UserTypeId", "Description", view.UserTypeId);
            return(View(view));
        }
Ejemplo n.º 4
0
 // GET: Users/Create
 public ActionResult Create()
 {
     ViewBag.UserTypeId = new SelectList(ComboHelper.GetUserTypeId(), "UserTypeId", "Description");
     return(View());
 }