コード例 #1
0
        public ActionResult AddNew(TheLoai obj, HttpPostedFileBase file)
        {
            try
            {
                if (file != null && file.ContentLength > 0)
                {
                    string fileName = Path.GetFileNameWithoutExtension(file.FileName);
                    fileName += "_" + obj.IdTheLoai;
                    fileName += Path.GetExtension(file.FileName);

                    string folderPath = Server.MapPath("~") + @"\Assets\images\genrs";

                    if (!Directory.Exists(folderPath))
                    {
                        Directory.CreateDirectory(folderPath);
                    }

                    string path = Path.Combine(folderPath, fileName);

                    file.SaveAs(path);
                    obj.AnhTheLoai = fileName;
                    int id = dao.AddNew(obj);
                    if (id != -1)
                    {
                        return(RedirectToAction("Index", "TheLoai"));
                    }
                    else
                    {
                        ViewBag.Message("Them moi khong thanh cong");
                        return(RedirectToAction("AddNew", "TheLoai"));
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Bạn phải nhập vào trường này");
                }
            }
            catch
            {
            }
            return(View());
        }