Exemple #1
0
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."));
            }

            if (!ModelState.IsValid)
            {
                return(Page());
            }
            var Userid      = _signInManager.Context.User.Claims.FirstOrDefault().Value;
            var emptyAdress = new Address();

            AddressVM.OwnerId     = Userid;
            AddressVM.Owner       = user;
            AddressVM.Name        = Input.Name;
            AddressVM.AddressLine = Input.AddressLine;

            /*if (await TryUpdateModelAsync<Address>(
             *  emptyAdress,
             *  "address",   // Prefix for form value.
             *  a => a.Name, a => a.AddressLine))
             * {*/

            var entry = _context.Add(new Address());

            entry.CurrentValues.SetValues(AddressVM);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./AddressIndex"));
            //}
        }
        private static void CreateData(ApplicationDbContext context)
        {
            context.Database.EnsureCreated();
            //Create Posts and comments

            //1st post
            if (context.Posts.Count() == 0)
            {
                Post p1 = new Post()
                {
                    Title     = "1st Post",
                    Content   = "Hello!",
                    Published = new DateTime(2018, 12, 31, 13, 22, 00),
                    Username  = "******"
                };
                context.Add(p1);

                context.Comments.Add(
                    new Comment()
                {
                    Content   = "Hi",
                    Published = new DateTime(2018, 12, 31, 13, 25, 00),
                    Username  = "******",
                    MyPost    = p1
                });

                context.Comments.Add(
                    new Comment()
                {
                    Content   = "Hello",
                    Published = new DateTime(2018, 12, 31, 13, 30, 00),
                    Username  = "******",
                    MyPost    = p1
                });

                context.Comments.Add(
                    new Comment()
                {
                    Content   = "Hello All!",
                    Published = new DateTime(2018, 12, 31, 13, 33, 00),
                    Username  = "******",
                    MyPost    = p1
                });

                //2nd Post
                Post p2 = new Post()
                {
                    Title     = "Happy New Year!",
                    Content   = "Happy new year everyone!",
                    Published = new DateTime(2019, 1, 1, 00, 1, 00),
                    Username  = "******"
                };
                context.Add(p2);

                context.Comments.Add(
                    new Comment()
                {
                    Content   = "HNY!",
                    Published = new DateTime(2019, 1, 1, 00, 5, 00),
                    Username  = "******",
                    MyPost    = p2
                });

                //3rd Post
                Post p3 = new Post()
                {
                    Title     = "How was everyone's holidays?",
                    Content   = "I had a great time and i hope everyone did as well",
                    Published = new DateTime(2019, 1, 3, 15, 33, 00),
                    Username  = "******"
                };
                context.Add(p3);

                context.Comments.Add(
                    new Comment()
                {
                    Content   = "It was ok, only got socks for christmas...Again :(",
                    Published = new DateTime(2019, 1, 3, 17, 33, 00),
                    Username  = "******",
                    MyPost    = p3
                });

                context.Comments.Add(
                    new Comment()
                {
                    Content   = "Not too bad",
                    Published = new DateTime(2019, 1, 3, 19, 33, 00),
                    Username  = "******",
                    MyPost    = p3
                });

                //4th Post
                Post p4 = new Post()
                {
                    Title     = "Revision Advice",
                    Content   = "Anyone have any revision tips they would like to share?",
                    Published = new DateTime(2019, 1, 4, 17, 33, 00),
                    Username  = "******"
                };
                context.Add(p4);

                context.Comments.Add(
                    new Comment()
                {
                    Content   = "No",
                    Published = new DateTime(2019, 1, 3, 17, 33, 00),
                    Username  = "******",
                    MyPost    = p4
                });

                context.SaveChanges();
            }
        }