コード例 #1
0
        public IActionResult OnGet()
        {
            var signedInUserID = this.User.FindFirstValue(ClaimTypes.NameIdentifier).ToString();

            using (ApplicationDbContext ctx = new ApplicationDbContext())
            {
                // If userID don't excist site will not load any userdata
                var getAdressinfo = from a in ctx.UserAdress
                                    where a.UserID == Guid.Parse(signedInUserID)
                                    select a;

                foreach (var item in getAdressinfo)
                {
                    AdressModel adress = new AdressModel();

                    adress.ID      = item.ID;
                    adress.Street  = item.Adress;
                    adress.ZipCode = Convert.ToInt32(item.ZipCode);
                    adress.City    = item.City;
                    adress.CareOf  = item.CareOf;
                    adress.UserID  = item.UserID;
                    adressList.Add(adress);
                }
                return(Page());
            }
        }
コード例 #2
0
        public IActionResult OnPost(string Adress, string CareOf, int ZipCode, string City)
        {
            using (ApplicationDbContext ctx = new ApplicationDbContext())
            {
                AdressModel adressModel    = new AdressModel();
                var         signedInUserID = this.User.FindFirstValue(ClaimTypes.NameIdentifier).ToString();

                var checkEmptyAdress = from e in ctx.UserAdress
                                       select e;
                if (checkEmptyAdress.Count() == 0)
                {
                    isAdressEmpty = true;
                }

                if (isAdressEmpty == true)
                {
                    var query = from user in ctx.UserAdress
                                where user.UserID == Guid.Parse(signedInUserID)
                                select user;

                    UserAdress ua = new UserAdress();
                    ua.Adress  = Adress;
                    ua.CareOf  = CareOf;
                    ua.ZipCode = ZipCode.ToString();
                    ua.City    = City;
                    ua.UserID  = Guid.Parse(signedInUserID);
                    ctx.UserAdress.Add(ua);
                    ctx.SaveChanges();
                }
                else
                {
                    var query = from user in ctx.UserAdress
                                where user.UserID == Guid.Parse(signedInUserID)
                                select user;

                    foreach (var adress in query)
                    {
                        adress.Adress  = Adress;
                        adress.CareOf  = CareOf;
                        adress.ZipCode = ZipCode.ToString();
                        adress.City    = City;
                        adress.UserID  = Guid.Parse(signedInUserID);
                    }
                    ctx.SaveChanges();
                }
            }
            return(Page());
        }