コード例 #1
0
ファイル: AccountController.cs プロジェクト: lequydon/.NETMVC
 public ActionResult Profile(Employee model, HttpPostedFileBase file)
 {
     //string temp = Request.Cookies["name"].Value;
     if (model.Email == null)
     {
         Account account = new Account();
         account.Email = User.GetUserData().UserID;
         UserProfile userProfile = UserAccountBLL.Account_GetEmployee(account, UserAccountTypes.Employee);
         return(View(userProfile));
     }
     else
     {
         if (string.IsNullOrEmpty(model.LastName))
         {
             ModelState.AddModelError("LastName", "Last Name required");
         }
         if (string.IsNullOrEmpty(model.FirstName))
         {
             ModelState.AddModelError("FirstName", "First Name required");
         }
         if (string.IsNullOrEmpty(model.City))
         {
             model.City = "";
         }
         if (string.IsNullOrEmpty(model.Address))
         {
             model.Address = "";
         }
         if (string.IsNullOrEmpty(model.Title))
         {
             model.Title = "";
         }
         if (string.IsNullOrEmpty(model.Country))
         {
             model.Country = "";
         }
         if (string.IsNullOrEmpty(model.Email))
         {
             model.Email = "";
         }
         if (string.IsNullOrEmpty(model.HomePhone))
         {
             model.HomePhone = "";
         }
         if (string.IsNullOrEmpty(model.Notes))
         {
             model.Notes = "";
         }
         var dateTime        = new DateTime(1900, 01, 01);
         var compareDatetime = DateTime.Compare(model.BirthDate, dateTime);
         if (compareDatetime < 0)
         {
             ModelState.AddModelError("BirthDate", "BirthDate is not format");
             return(View(model));
         }
         compareDatetime = DateTime.Compare(model.HireDate, dateTime);
         if (compareDatetime < 0)
         {
             ModelState.AddModelError("HireDate", "HireDate is not format");
             return(View(model));
         }
         if (string.IsNullOrEmpty(model.Password))
         {
             ModelState.AddModelError("Password", "Password required");
         }
         var fileName = "";
         var typeFile = "";
         if (file != null)
         {
             //kiểm tra loại của file
             fileName = Path.GetFileName(file.FileName);
             typeFile = fileName.Substring(fileName.IndexOf('.'));
             if (typeFile != ".png" && typeFile != ".jpg" && typeFile != ".jpeg" && typeFile != ".JPG" && typeFile != ".PNG" && typeFile != ".JPEG")
             {
                 ModelState.AddModelError("pathFile", "File is not image");
                 return(View(model));
             }
         }
         else
         {
             model.PhotoPath = "";
         }
         if (!ModelState.IsValid)
         {
             return(View(model));
         }
         try
         {
             bool    updateResult = UserAccountBLL.Employee_Update(model, file, UserAccountTypes.Employee);
             Account account      = new Account();
             account.Email = model.Email;
             UserProfile userProfile = UserAccountBLL.Account_GetEmployee(account, UserAccountTypes.Employee);
             //set cookie
             WebUserData cookieData = new WebUserData()
             {
                 UserID    = userProfile.Email,
                 FullName  = userProfile.FirstName + " " + userProfile.LastName,
                 GroupName = userProfile.GroupName,
                 SessionID = Session.SessionID,
                 ClientIP  = Request.UserHostAddress,
                 Photo     = userProfile.PhotoPath
             };
             FormsAuthentication.SetAuthCookie(cookieData.ToCookieString(), false);
             //var nameCoockie = new HttpCookie("name");
             //nameCoockie.Expires = DateTime.Now.AddDays(-1);
             //Response.Cookies.Add(nameCoockie);
             //Response.Cookies["name"].Value = AccountBLL.Account_GetEmployee(account).FirstName + " " + AccountBLL.Account_GetEmployee(account).LastName;
             //var photoPathCoockie = new HttpCookie("photoPath");
             //photoPathCoockie.Expires = DateTime.Now.AddDays(-1);
             //Response.Cookies.Add(photoPathCoockie);
             //Response.Cookies["photoPath"].Value = AccountBLL.Account_GetEmployee(account).PhotoPath;
             return(View(userProfile));
         }
         catch (Exception ex)
         {
             ModelState.AddModelError("", ex.Message + ": " + ex.StackTrace);
             return(View());
         }
     }
 }