public ActionResult Categories(Category cat)
 {
     if (ModelState.IsValid)
     {
         db.Categories.Add(cat);
         db.SaveChanges();
     }
     ViewBag.Categories = db.Categories;
     return(View(cat));
 }
Beispiel #2
0
        public ActionResult Edit(User user)
        {
            if (ModelState.IsValid)
            {
                db.Entry(user).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            SelectList roles = new SelectList(db.Roles, "Id", "Name");

            ViewBag.Roles = roles;

            return(View(user));
        }
Beispiel #3
0
        public ActionResult Create(Post post, HttpPostedFileBase picture)
        {
            // получаем текущего пользователя
            User user = db.Users.Where(m => m.Login == HttpContext.User.Identity.Name).FirstOrDefault();

            if (user == null)
            {
                return(RedirectToAction("LogOff", "Account"));
            }
            if (ModelState.IsValid)
            {
                // указываем статус добавлен у поста
                post.Status = (int)PostStatus.Sent;
                //получаем время открытия
                DateTime current = DateTime.Now;

                //Создаем запись о жизненном цикле поста
                Lifecycle newLifecycle = new Lifecycle()
                {
                    Sent = current
                };
                post.Lifecycle = newLifecycle;

                //Добавляем жизненный цикл поста
                db.Lifecycles.Add(newLifecycle);

                // указываем пользователя поста
                post.UserId = user.Id;

                // если получен файл
                if (picture != null)
                {
                    // Получаем расширение
                    string ext = picture.FileName.Substring(picture.FileName.LastIndexOf('.'));
                    // сохраняем файл по определенному пути на сервере
                    string path = current.ToString("dd/MM/yyyy H:mm:ss").Replace(":", "_").Replace("/", ".") + ext;
                    picture.SaveAs(Server.MapPath("~/Uploads/" + path));
                    post.File = path;
                }
                //Добавляем пост в БД
                db.Posts.Add(post);
                db.SaveChanges();

                return(RedirectToAction("Index"));
            }
            return(View(post));
        }
Beispiel #4
0
        public ActionResult Register(User user)
        {
            int id   = 3;
            var role = db.Roles.Find(id);

            if (role != null)
            {
                user.RoleId = role.Id;
            }
            user.Password = Crypto.HashPassword(user.Password);

            if (ModelState.IsValid)
            {
                db.Users.Add(user);
                db.SaveChanges();
                return(RedirectToAction("Index", "User"));
            }

            return(View(user));
        }