//Optional parameter.
        public ViewResult Details(int Id = -1)
        {
            UserProfile userprofile = null;
            if (Id == -1) //If no user was specified, then go to user logged in.
            {
                if (Membership.GetUser() == null) //no one logged in. Show index.
                {
                    return View("~/Views/UserProfile/M/Index.cshtml", db.UserProfiles.ToList());
                }
                Guid userId = (Guid)Membership.GetUser().ProviderUserKey;

                foreach (UserProfile u in db.UserProfiles.ToList())
                {
                    if (u.AccountId != null)
                    {
                        if (u.AccountId == userId.ToString())
                        {
                            userprofile = u;
                            break;
                        }
                    }
                }
                if (userprofile == null && userId != null)
                {
                    //create a userprofile for this user.
                    userprofile = new UserProfile();
                    userprofile.AccountId = userId.ToString();
                    userprofile.Question = Membership.GetUser(userId).PasswordQuestion;
                    userprofile.Answer = "";
                    userprofile.Description = "Descripion here";
                    userprofile.Department = "Department here";
                    userprofile.DisplayName = Membership.GetUser(userId).UserName;
                    userprofile.Picture = "Unknown.jpg";
                    userprofile.Year = 0;
                    db.UserProfiles.Add(userprofile);
                    db.SaveChanges();
                }
            }
            else
            {
                userprofile = db.UserProfiles.Find(Id);
            }
            return View(userprofile);
        }
예제 #2
0
        public ActionResult Register(RegisterModel model)
        {
            //if (ModelState.IsValid)
            //{
            //Attempt to register the user
            MembershipCreateStatus createStatus;
            Guid id = Guid.NewGuid();
            Membership.CreateUser(model.UserName, model.Password, model.Email, model.Question, model.Answer, true, id, out createStatus);

            FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);

            if (createStatus == MembershipCreateStatus.Success)
            {
                UserProfile temp = new UserProfile();
                string filePath = null;
                try
                {
                    var tempImage = Bitmap.FromStream(model.Picture.InputStream); //This will throw an exception if not real
                    filePath = Server.MapPath("\\PICTURES\\") + id.ToString() + Path.GetExtension(model.Picture.FileName); // Picture will be the Users AccountID number plus the extension
                    if (System.IO.File.Exists(filePath))
                    {
                        //If the file already exists for some reason, delete
                        System.IO.File.Delete(filePath);
                    }
                    model.Picture.SaveAs(filePath);
                }
                catch (Exception e)
                {
                    //A valid picture wasen't uploaded, could be a malicious script. Use defaut picture
                    filePath = "\\Unknown.jpg"; //Default picture
                }

                temp.Department = model.Department;
                temp.Description = model.Description;
                temp.Picture = filePath.Split('\\')[filePath.Split('\\').Length - 1];
                temp.AccountId = id.ToString();
                temp.Year = model.Year;
                temp.DisplayName = model.DisplayName;
                temp.Question = model.Question;
                temp.Answer = model.Answer;

                HomeController.newsDB.UserProfiles.Add(temp);
                HomeController.newsDB.SaveChanges();
                return RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError("", ErrorCodeToString(createStatus));
            }
            //}

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        public ActionResult Edit(UserProfile userprofile)
        {
            if (ModelState.IsValid)
            {
                //string filePath = Server.MapPath("\\PICTURES\\") + Path.GetFileName(userprofile.PictureUpload.FileName);
                ////   System.IO.File.Create(filePath);
                //userprofile.PictureUpload.SaveAs(filePath);
                //userprofile.Picture = userprofile.PictureUpload.FileName;
                db.Entry(userprofile).State = EntityState.Modified;
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            return View(userprofile);
        }
 //
 // GET: /UserProfile/Edit/5
 public ActionResult Edit()
 {
     if (Membership.GetUser() != null) //if logged in
     {
         Guid userId = (Guid)Membership.GetUser().ProviderUserKey;
         UserProfile userprofile = null;
         foreach (UserProfile u in db.UserProfiles.ToList())
         {
             if (u.AccountId != null)
             {
                 if (u.AccountId == userId.ToString())
                 {
                     userprofile = u;
                     break;
                 }
             }
         }
         if (userprofile == null && userId != null)
         {
             //create a userprofile for this user.
             userprofile = new UserProfile();
             userprofile.AccountId = userId.ToString();
             userprofile.Question = Membership.GetUser(userId).PasswordQuestion;
             userprofile.Answer = "";
             userprofile.Description = "Descripion here";
             userprofile.Department = "Department here";
             userprofile.DisplayName = Membership.GetUser(userId).UserName;
             userprofile.Picture = "Unknown.jpg";
             userprofile.Year = 0;
             db.UserProfiles.Add(userprofile);
             db.SaveChanges();
         }
         return View(userprofile);
     }
     else
     {
         return RedirectToAction("LogOn", "Account");
     }
 }