Ejemplo n.º 1
0
        public async Task <ActionResult> DeleteHamlet(ViewModels.EditHamlet updateHamlet)
        {
            if (updateHamlet != null)
            {
                var currentUser = await db.Users.Where(x => x.UserName == User.Identity.Name)
                                  .SingleOrDefaultAsync();

                if (currentUser != null)
                {
                    var hamlet = new Models.Hamlet
                    {
                        IsDeleted = true,
                        IsActive  = false,
                        DeletedBy = currentUser,
                        Deleted   = DateTimeOffset.UtcNow
                    };
                    try
                    {
                        db.Entry(hamlet).State = EntityState.Modified;
                        var result = await db.SaveChangesAsync();

                        if (result > 0)
                        {
                            return(RedirectToAction("Hamlet", "VillageHead"));
                        }
                    }
                    catch (Exception ex)
                    {
                        Trace.TraceError(ex.Message);
                        Trace.TraceError(ex.StackTrace);
                    }
                }
            }
            return(View("Error"));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> AddHamlet(ViewModels.AddHamlet newHamlet)
        {
            if (ModelState.IsValid)
            {
                var currentUser = await db.Users.Where(x => x.UserName == User.Identity.Name)
                                  .SingleOrDefaultAsync();

                if (currentUser != null)
                {
                    var addHamlet = new Models.Hamlet
                    {
                        IdHamlet  = newHamlet.IdHamlet,
                        Name      = newHamlet.Name,
                        Chairman  = newHamlet.Chairman,
                        IsActive  = true,
                        CreatedBy = currentUser,
                        Created   = DateTimeOffset.UtcNow
                    };
                    try
                    {
                        db.Hamlets.Add(addHamlet);
                        var result = await db.SaveChangesAsync();

                        if (result > 0)
                        {
                            return(RedirectToAction("Hamlet", "VillageHead"));
                        }
                    }
                    catch (Exception ex)
                    {
                        Trace.TraceError(ex.Message);
                        Trace.TraceError(ex.StackTrace);
                    }
                }
            }
            return(View("Error"));
        }