public ActionResult MakeOrder() { dbMSA3Entities cont = new dbMSA3Entities(); var productsUser = new ProductsAndUserModel(); //Contains a list of ID's, we'll use this to find the correct products var cartlist = (List <int>)Session["CartList"]; foreach (int itm in cartlist) { //Find and save the product linked to the ID Products obj = new Products(); obj = cont.Products.Single(x => x.ProductID == itm); productsUser.Products.Add(obj); } //Establish a connection so we can find the logged in user ApplicationDbContext context = new ApplicationDbContext(); var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context)); var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context)); //Save the user to a variable, so we can extract the needed attributes var user = UserManager.FindById(User.Identity.GetUserId()); productsUser.Address = user.Adress; productsUser.City = user.City; productsUser.ZipCode = user.ZipCode; return(View(productsUser)); }
public ActionResult Edit([Bind(Include = "UserID,UserName,Email,Adress,City,ZipCode")] Users users) { //If email is changed, verify that it is not already in use //Update own database then identity database dbMSA3Entities cont = new dbMSA3Entities(); List <Users> userlist = new List <Users>(); ViewBag.ErrorMsg = ""; userlist = db.Users.SqlQuery("Select * from Users where Users.Email ='" + users.Email + "' AND NOT (Users.UserID = '" + users.UserID + "')").ToList(); if (userlist.Count > 0) { ViewBag.ErrorMsg = "The Email is already in use"; return(View()); } //Update own database table Users try { db.Users.SqlQuery("Update Users SET Email = '" + users.Email + "',UserName = '******',Adress = '" + users.Adress + "',City = '" + users.City + "',ZipCode = '" + users.ZipCode + "' WHERE Users.UserID = '" + users.UserID + "'"); db.SaveChanges(); } catch (Exception e) { ViewBag.ErrorMsg = "Unable to update database. Errormessage:" + e.Message.ToString(); return(View()); } //Update the identity database if (ModelState.IsValid) { db.Entry(users).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(users)); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Adress = model.Adress, City = model.City, ZipCode = model.ZipCode }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { //find userID var userid = UserManager.FindByEmail(user.Email).Id; dbMSA3Entities cont = new dbMSA3Entities(); Users obj = new Users(); obj.UserID = userid; obj.UserName = user.UserName; obj.Email = user.Email; obj.Adress = user.Adress; obj.City = user.City; obj.ZipCode = user.ZipCode; cont.Users.Add(obj); cont.SaveChanges(); await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); return(RedirectToAction("Index", "Products")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public ActionResult ShopingCartDetails() { dbMSA3Entities cont = new dbMSA3Entities(); List <Products> plist = new List <Products>(); var cartlist = (List <int>)Session["CartList"]; foreach (int itm in cartlist) { Products obj = new Products(); obj = cont.Products.Single(x => x.ProductID == itm); plist.Add(obj); } return(View(plist)); }
public ActionResult ShopingCart() { dbMSA3Entities cont = new dbMSA3Entities(); List <ProductCategoriesViewModel> productCategoryList = new List <ProductCategoriesViewModel>(); if (Session["CartList"] == null) { List <int> cartList = new List <int>(); Session["CartList"] = cartList; } //Contains a list of ID's, we'll use this to find the correct products var cartlist = (List <int>)Session["CartList"]; foreach (int itm in cartlist) { List <Categories> categories = new List <Categories>(); //Find and save the product linked to the ID Products obj = new Products(); obj = cont.Products.Single(x => x.ProductID == itm); //find each category "linked" to the product, and add it to our category list foreach (var prodCat in cont.ProdCat.Where(x => x.ProductID == obj.ProductID)) { //This query will find the category IEnumerable <Categories> query = from c in cont.Categories where c.CategoryID == prodCat.CategoryID select c; //for each category, add it to a temporary list, so we can include it into our final list foreach (Categories category in query) { categories.Add(category); } } productCategoryList.Add(new ProductCategoriesViewModel(obj.ProductID, obj.ProductName, obj.Price, obj.ProductTypeID, categories)); } return(View(productCategoryList)); }
private void createRolesandUsers() { ////create sessionobject for shopingcart //List<int> cartList = new List<int>(); //HttpContext.Current.Session["CartList"] = "test"; ////Session["products"] = null; //// When retrieving an object from session state, cast it to //// the appropriate type. //ArrayList stockPicks = (ArrayList)Session["StockPicks"]; //// Write the modified stock picks list back to session state. //Session["StockPicks"] = stockPicks; ApplicationDbContext context = new ApplicationDbContext(); var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context)); var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context)); // In Startup iam creating first Admin Role and creating a default Admin User if (!roleManager.RoleExists("Admin")) { // create admin roole var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole(); role.Name = "Admin"; roleManager.Create(role); //create default admin user var user = new ApplicationUser(); user.UserName = "******"; user.Email = "*****@*****.**"; user.Adress = "TESTadress"; user.City = "Krallköping"; user.ZipCode = "1212"; string userPWD = "@Lexicon11"; var chkUser = UserManager.Create(user, userPWD); //find userID var userid = UserManager.FindByEmail(user.Email).Id; dbMSA3Entities cont = new dbMSA3Entities(); Users obj = new Users(); obj.UserID = userid; obj.UserName = user.UserName; obj.Email = user.Email; obj.Adress = user.Adress; obj.City = user.City; obj.ZipCode = user.ZipCode; cont.Users.Add(obj); cont.SaveChanges(); //Add Stefan to Role Admin if (chkUser.Succeeded) { var result1 = UserManager.AddToRole(user.Id, "Admin"); } } }