コード例 #1
0
        public ActionResult Create([Bind(Include = "OwnerAssetID,AssetName,OwnerID,AssetPhoto,SpecialNotes,IsActive,DateAdded")]
                                   OwnerAsset ownerAsset, HttpPostedFileBase AssetPhoto)
        {
            if (ModelState.IsValid)
            {
                ownerAsset.OwnerID = User.Identity.GetUserId();

                //***********  USER INFO AND FILE IMAGE UPLOAD
                #region User Information and File/Image Upload
                //default image will be noImage.jpg if no image is provided
                string image = "noImage.jpg";

                //check that image upload contains valid image
                if (AssetPhoto != null)
                {
                    //yes
                    //reassign the fileName to the variable that represents the default img
                    image = AssetPhoto.FileName;

                    //create a variable and retrieve the extension from the image
                    string ext = image.Substring(image.LastIndexOf("."));

                    //create a list of valid file extensions - (whitelist)
                    string[] goodExts = new string[] { ".jpg", ".png", ".jpeg", ".gif" };

                    //check our extension against that list
                    if (goodExts.Contains(ext.ToLower()))
                    {
                        //as long as our extension is in that list
                        //rename the file to a unique file name and add the extension
                        image = Guid.NewGuid() + ext;
                        //save the new file to the website

                        AssetPhoto.SaveAs(Server.MapPath("~/Content/assets/images/UserImages/" + image));
                    }
                    //if an invalid extension is provided
                    else
                    {
                        //go back to the default page
                        image = "noImage.jpg";
                    }
                }

                //No Matter What add the image name to the database object
                ownerAsset.AssetPhoto = image;
                #endregion

                ownerAsset.DateAdded = DateTime.Now;
                ownerAsset.IsActive  = true;
                db.OwnerAssets.Add(ownerAsset);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(ownerAsset));
        }
        public async Task <ActionResult> Create(RegisterViewModel userViewModel,
                                                params string[] selectedRoles)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = userViewModel.Email, Email = userViewModel.Email
                };
                var adminresult = await UserManager.CreateAsync(user, userViewModel.Password);

                //Add User to the selected Roles
                if (adminresult.Succeeded)
                {
                    if (selectedRoles != null)
                    {
                        var result = await UserManager.AddToRolesAsync(user.Id, selectedRoles);

                        if (!result.Succeeded)
                        {
                            ModelState.AddModelError("", result.Errors.First());
                            ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync(), "Name", "Name");
                        }
                    }
                    #region Create user object

                    //No Matter What add the image name to the database object
                    //Create user object as well
                    UserDetail info = new UserDetail()
                    {
                        UserID    = user.Id,
                        FirstName = userViewModel.FirstName,
                        LastName  = userViewModel.LastName,
                    };
                    //add the UserDetails Object to EF
                    ctx.UserDetails.Add(info);
                    //Sent the object to the database as a record
                    ctx.SaveChanges();
                    var users = await UserManager.Users.ToListAsync();

                    return(RedirectToAction("Index", users));

                    #endregion
                }

                else
                {
                    ModelState.AddModelError("", adminresult.Errors.First());
                    ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name");
                    return(View());
                }
                //return RedirectToAction("Index");
            }
            ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name");
            return(View());
        }
コード例 #3
0
        public ActionResult Create([Bind(Include = "UserID,FirstName,LastName")] UserDetail userDetail)
        {
            if (ModelState.IsValid)
            {
                db.UserDetails.Add(userDetail);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(userDetail));
        }
        public ActionResult Create([Bind(Include = "ServicesProvidedID,ServicesProvided1")] ServicesProvided servicesProvided)
        {
            if (ModelState.IsValid)
            {
                db.ServicesProvideds.Add(servicesProvided);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(servicesProvided));
        }
コード例 #5
0
        public ActionResult Create([Bind(Include = "LocationID,LocationName,Address,City,State,ZipCode,PhoneNumber,Email,ReservationLimit")] Location location)
        {
            if (ModelState.IsValid)
            {
                db.Locations.Add(location);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(location));
        }
        public ActionResult Create([Bind(Include = "ReservationID,LocationID,ReservationDate,OwnerAssetID,ServicesProvidedID,Notes")]
                                   Reservation reservation)
        {
            if (ModelState.IsValid)
            {
                var location = db.Locations.Where(abo => abo.LocationID == reservation.LocationID).Single();
                if ((location.ReservationLimit > location.Reservations.Where(x => x.ReservationDate == reservation.ReservationDate).Count()) || User.IsInRole("Admin"))
                {
                    db.Reservations.Add(reservation);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                return(View("ErrorPageResLimitExceeded"));
            }

            ViewBag.LocationID         = new SelectList(db.Locations, "LocationID", "LocationName", reservation.LocationID);
            ViewBag.ServicesProvidedID = new SelectList(db.ServicesProvideds, "ServicesProvidedID", "ServicesProvided1", reservation.ServicesProvidedID);
            ViewBag.OwnerAssetID       = new SelectList(db.OwnerAssets, "OwnerAssetID", "AssetName", reservation.OwnerAssetID);
            return(View(reservation));
        }
コード例 #7
0
        [ValidateAntiForgeryToken]//created a new view for Register - overriding the old view
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    #region Assigning customer a role of user and Creating User Object

                    //Added to assign customers a role of user when registering.
                    UserManager.AddToRole(user.Id, "User");
                    //***********  USER INFO

                    //Create user object as well
                    UserDetail info = new UserDetail()
                    {
                        UserID    = user.Id,
                        FirstName = model.FirstName,
                        LastName  = model.LastName,
                    };
                    //Create the context to get to EF
                    PawsNClawsEntities ctx = new PawsNClawsEntities();
                    //add the UserDetails Object to EF
                    ctx.UserDetails.Add(info);
                    //Sent the object to the database as a record
                    ctx.SaveChanges();
                    #endregion

                    var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                    var callbackUrl = Url.Action("ConfirmEmail", "Account",
                                                 new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    await UserManager.SendEmailAsync(user.Id,
                                                     "Confirm your account", "Please confirm your account by clicking this link: <a href=\""
                                                     + callbackUrl + "\">link</a>");

                    ViewBag.Link = callbackUrl;
                    return(View("DisplayEmail"));
                }
                AddErrors(result);
            }
            // If we got this far, something failed, redisplay form
            return(View(model));
        }