コード例 #1
0
 public bool DeleteStudentDetail(int id)
 {
     try
     {
         if (id > 0)
         {
             SampleDatabaseContext db = new SampleDatabaseContext();
             Student studentObj       = db.Students.Where(a => a.StudentID == id).FirstOrDefault();
             if (studentObj != null)
             {
                 db.Entry <Student>(studentObj).State = EntityState.Deleted;
                 db.SaveChanges();
                 return(true);
             }
             return(false);
         }
         else
         {
             return(false);
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
コード例 #2
0
        public IActionResult Create(Blog blog)
        {
            if (ModelState.IsValid)
            {
                _context.Blog.Add(blog);
                _context.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(blog));
        }
コード例 #3
0
        public JsonResult SignUp(string register_username, string register_email, string register_password)
        {
            ModalLoginJsonResult result = new ModalLoginJsonResult();

            register_username = register_username?.Trim();
            register_email    = register_email?.Trim();
            register_password = register_password?.Trim();

            if (string.IsNullOrEmpty(register_username) || string.IsNullOrEmpty(register_email) || string.IsNullOrEmpty(register_password))
            {
                result.HasError = true;
                result.Result   = "Please, fill all blank fields.";
            }
            else
            {
                LoginUser user = db.LoginUsers.FirstOrDefault(x => x.Username == register_username || x.Email == register_email);

                if (user != null)
                {
                    result.HasError = true;
                    result.Result   = "Username or e-mail address to be used.";
                }
                else
                {
                    user = db.LoginUsers.Add(new LoginUser()
                    {
                        Name     = string.Empty,
                        Surname  = string.Empty,
                        Email    = register_email,
                        Username = register_username,
                        Password = register_password
                    });

                    if (db.SaveChanges() > 0)
                    {
                        result.HasError = false;
                        result.Result   = "Account created.";

                        // Detached : This should be used for example if you want to load entity only to read data and you don't plan to modify them.
                        db.Entry(user).State = System.Data.Entity.EntityState.Detached;

                        user.Password = string.Empty;   // Session is not include pass for security.

                        // Set login to session for auto login from register.
                        Session["login"] = user;
                    }
                    else
                    {
                        result.HasError = true;
                        result.Result   = "Error occured.";
                    }
                }
            }


            return(Json(result, JsonRequestBehavior.AllowGet));
        }
コード例 #4
0
        public JsonResult UpdatePageField(string id, string val, string pname)
        {
            if (Session["login"] == null || Session["login"] as LoginUser == null)
            {
                return(Json(new MyJsonResult <string>(true, "Unauthorize access.")));
            }

            LoginUser user = Session["login"] as LoginUser;

            //// You can add RoleName prop to LoginUser class and use it.
            //if (user.RoleName != "editor" && (user.RoleName != "*"))
            //{
            //    return Json(new MyJsonResult<string>(true, "Unauthorize access."));
            //}

            SampleDatabaseContext db    = new SampleDatabaseContext();
            BasicPageField        field = db.BasicPageFields.FirstOrDefault(x => x.Id == id);

            if (field == null)
            {
                field          = new BasicPageField();
                field.Id       = id;
                field.PageName = pname;
                db.BasicPageFields.Add(field);
            }

            field.LastModifiedDate = DateTime.Now;
            field.LastModifiedUser = user.Username;
            field.Text             = val;

            if (db.SaveChanges() > 0)
            {
                return(Json(new MyJsonResult <string>(false, "Update successful.")));
            }
            else
            {
                return(Json(new MyJsonResult <string>(false, "Update fail.")));
            }
        }
コード例 #5
0
        public Student SaveStudentDetail(int?id, Student student)
        {
            SampleDatabaseContext db = new SampleDatabaseContext();

            try
            {
                if (id != null && id.Value > 0)
                {
                    db.Entry <Student>(student).State = EntityState.Modified;
                }
                else
                {
                    db.Entry <Student>(student).State = EntityState.Added;
                }
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(student);
        }
コード例 #6
0
        public JsonResult LostPassword(string lost_email)
        {
            ModalLoginJsonResult result = new ModalLoginJsonResult();

            lost_email = lost_email?.Trim();

            if (string.IsNullOrEmpty(lost_email))
            {
                result.HasError = true;
                result.Result   = "E-Mail address can not be empty.";
            }
            else
            {
                // TODO : KMB Modal Login - Lost Password
                LoginUser user = db.LoginUsers.FirstOrDefault(x => x.Email == lost_email);

                if (user != null)
                {
                    //
                    // TODO : Send password with e-mail.
                    // Reads mail settings from AppSettings into web.config file.
                    //

                    #region Sends password to user mail address.
                    // Sends password to user mail address.
                    //bool sent = mailer.SendMail($"<b>Your password :</b> {user.Password}",
                    //user.Email, "Your missed password", true);

                    //if (sent == false)
                    //{
                    //    result.HasError = true;
                    //    result.Result = "Password has not been sent.";
                    //}
                    //else
                    //{
                    //    result.HasError = false;
                    //    result.Result = "Password has been sent.";
                    //}
                    #endregion


                    #region Sends password reset link to user mail address.
                    // Sends password reset link to user mail address.
                    user.LostPasswordToken = Guid.NewGuid();

                    if (db.SaveChanges() > 0)
                    {
                        bool sent = mailer.SendMail(
                            $"<b>Your reset password link :</b> <a href='http://{Request.Url.Authority}/ModalLogin/ResetPassword/{user.LostPasswordToken}' target='_blank'>Reset Password</a>",
                            user.Email, "Reset Password", true);

                        if (sent == false)
                        {
                            result.HasError = true;
                            result.Result   = "Reset Password link has not been sent.";
                        }
                        else
                        {
                            result.HasError = false;
                            result.Result   = "Reset Password link has been sent.";
                        }
                    }
                    else
                    {
                        result.HasError = true;
                        result.Result   = "Error occured.";
                    }

                    #endregion
                }
                else
                {
                    result.HasError = true;
                    result.Result   = "E-Mail address not found.";
                }
            }

            return(Json(result, JsonRequestBehavior.AllowGet));
        }