public ActionResult Create([Bind(Include = "Id,City,Street")] Address address)
        {
            if (ModelState.IsValid)
            {
                db.Addresses.Add(address);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(address));
        }
示例#2
0
        public ActionResult Create([Bind(Include = "Id,Name,AddressId")] Studio studio)
        {
            if (ModelState.IsValid)
            {
                db.Studios.Add(studio);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.AddressId = new SelectList(db.Addresses, "Id", "City", studio.AddressId);
            return(View(studio));
        }
示例#3
0
        public ActionResult Create([Bind(Include = "Id,Content,AuthorId")] Tweet tweet)
        {
            if (ModelState.IsValid)
            {
                db.Tweets.Add(tweet);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.AuthorId = new SelectList(db.Users, "Id", "UserName", tweet.AuthorId);
            return(View(tweet));
        }
示例#4
0
        public ActionResult Create([Bind(Include = "Id,Name,Age,StudioId,AddressId")] Person person)
        {
            if (ModelState.IsValid)
            {
                db.People.Add(person);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.AddressId = new SelectList(db.Addresses, "Id", "City", person.AddressId);
            ViewBag.StudioId  = new SelectList(db.Studios, "Id", "Name", person.StudioId);
            return(View(person));
        }
示例#5
0
        public ActionResult Create([Bind(Include = "Id,Date,Title,DirectorId,LeadingMaleRoleId,LeadingFemaleRoleId")] Movie movie)
        {
            if (ModelState.IsValid)
            {
                db.Movies.Add(movie);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.DirectorId          = new SelectList(db.People, "Id", "Name", movie.DirectorId);
            ViewBag.LeadingFemaleRoleId = new SelectList(db.People, "Id", "Name", movie.LeadingFemaleRoleId);
            ViewBag.LeadingMaleRoleId   = new SelectList(db.People, "Id", "Name", movie.LeadingMaleRoleId);
            return(View(movie));
        }
示例#6
0
        private static void SeedAdmin(SimpleTwitterDbContext context)
        {
            if (!context.Roles.Any(r => r.Name == "Admin"))
            {
                var store   = new RoleStore <IdentityRole>(context);
                var manager = new RoleManager <IdentityRole>(store);
                var role    = new IdentityRole {
                    Name = "Admin"
                };

                manager.Create(role);
            }

            if (!context.Users.Any(u => u.UserName == "admin"))
            {
                var store   = new UserStore <User>(context);
                var manager = new ApplicationUserManager(store);
                var user    = new User
                {
                    UserName      = "******",
                    PasswordHash  = new PasswordHasher().HashPassword("admin"),
                    SecurityStamp = Guid.NewGuid().ToString()
                };

                context.Users.AddOrUpdate(user);
                context.SaveChanges();
                manager.AddToRole(user.Id, "Admin");
            }

            if (context.Users.Count() <= 1)
            {
                var random = new Random();
                for (int i = 1; i <= 5; i++)
                {
                    var user = new User()
                    {
                        UserName      = $"u{i}",
                        PasswordHash  = new PasswordHasher().HashPassword($"u{i}"),
                        SecurityStamp = Guid.NewGuid().ToString()
                    };

                    context.Users.AddOrUpdate(user);
                }

                context.SaveChanges();
            }
        }