public ActionResult Create([Bind(Include = "HA_IDHinhAnh,HD_IDHoatDong,HA_ChuThich,HA_NoiDung")] HinhAnhHoatDong hinhAnhHoatDong)
        {
            //int datalength = (int)Request.Files["image"].InputStream.Length;
            //byte[] _byteArr = new byte[datalength];
            //Request.Files[0].InputStream.Read(_byteArr, 0, datalength);
            var Image = Request.Files["Image"];
            var path  = Server.MapPath("~/Images/" + Image.FileName);

            Image.SaveAs(path);
            var sl = from p in db.HinhAnhHoatDongs select p;
            int a  = KTTT(sl.Count());

            if (ModelState.IsValid)
            {
                hinhAnhHoatDong.HA_IDHinhAnh = a;
                hinhAnhHoatDong.HA_NoiDung   = "/Images/" + Image.FileName;
                //hinhAnhHoatDong.HA_NoiDung = _byteArr;
                db.HinhAnhHoatDongs.Add(hinhAnhHoatDong);
                db.SaveChanges();
                //ModelState.AddModelError("", "Xong");
                return(RedirectToAction("Index"));
            }

            ViewBag.HD_IDHoatDong = new SelectList(db.HoatDongs, "HD_IDHoatDong", "HD_ChuDe", hinhAnhHoatDong.HD_IDHoatDong);
            return(View(hinhAnhHoatDong));
        }
        public ActionResult DeleteConfirmed(int id)
        {
            HinhAnhHoatDong hinhAnhHoatDong = db.HinhAnhHoatDongs.Find(id);

            db.HinhAnhHoatDongs.Remove(hinhAnhHoatDong);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
 public ActionResult Edit([Bind(Include = "HA_IDHinhAnh,HD_IDHoatDong,HA_ChuThich,HA_NoiDung")] HinhAnhHoatDong hinhAnhHoatDong)
 {
     if (ModelState.IsValid)
     {
         db.Entry(hinhAnhHoatDong).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.HD_IDHoatDong = new SelectList(db.HoatDongs, "HD_IDHoatDong", "HD_ChuDe", hinhAnhHoatDong.HD_IDHoatDong);
     return(View(hinhAnhHoatDong));
 }
        // GET: HinhAnhHoatDongs/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            HinhAnhHoatDong hinhAnhHoatDong = db.HinhAnhHoatDongs.Find(id);

            if (hinhAnhHoatDong == null)
            {
                return(HttpNotFound());
            }
            return(View(hinhAnhHoatDong));
        }
        //public ActionResult getImage(string id)
        //{
        //    string strID = Request.QueryString["ID"];
        //    int ID = -1;
        //    if (int.TryParse(id, out ID))
        //    {
        //        //var ha = db.HinhAnhHoatDongs.Where(h => h.HD_IDHoatDong == ID).FirstOrDefault();
        //        var ha = from p in db.HinhAnhHoatDongs where p.HA_IDHinhAnh == ID select p;
        //        foreach(var i in ha) {
        //        if (i == null || i.HA_NoiDung == null)
        //        {
        //            ModelState.AddModelError("", "Loi");

        //        }
        //            ViewBag.ha = i.HA_NoiDung;
        //        //Response.ContentType = "image/jpeg";
        //        Response.OutputStream.Write(i.HA_NoiDung.ToArray(), 0, i.HA_NoiDung.Length);
        //        Response.Flush();}
        //    }

        //    return View();
        //}
        // GET: HinhAnhHoatDongs/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            HinhAnhHoatDong hinhAnhHoatDong = db.HinhAnhHoatDongs.Find(id);

            if (hinhAnhHoatDong == null)
            {
                return(HttpNotFound());
            }
            ViewBag.HD_IDHoatDong = new SelectList(db.HoatDongs, "HD_IDHoatDong", "HD_ChuDe", hinhAnhHoatDong.HD_IDHoatDong);
            return(View(hinhAnhHoatDong));
        }
        // GET: HinhAnhHoatDongs/Delete/5
        public ActionResult Delete(int?id)
        {
            HinhAnhHoatDong hinhAnhHoatDong = db.HinhAnhHoatDongs.Find(id);

            db.HinhAnhHoatDongs.Remove(hinhAnhHoatDong);
            db.SaveChanges();
            return(RedirectToAction("Index"));
            //if (id == null)
            //{
            //    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            //}
            //HinhAnhHoatDong hinhAnhHoatDong = db.HinhAnhHoatDongs.Find(id);
            //if (hinhAnhHoatDong == null)
            //{
            //    return HttpNotFound();
            //}
            //return View(hinhAnhHoatDong);
        }
Exemplo n.º 7
0
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int ID = 0;

        int.TryParse(Request.QueryString["ID"], out ID);
        DBEntities db = new DBEntities();

        string Link = imageLink.Value;

        if (Link == String.Empty)
        {
            message.Visible   = true;
            message.InnerHtml = "Chọn ảnh đăng";
            return;
        }
        bool Show = false;

        if (checkbox_Show.Checked == true)
        {
            Show = true;
        }

        if (ID == 0)
        {
            using (db)
            {
                var newItem = new HinhAnhHoatDong();
                newItem.Name_Ext = Link.Replace(@"C:\fakepath\", @"\Img\System\").Replace(@"\\", @"\");
                newItem.Show     = Show;

                try
                {
                    db.HinhAnhHoatDongs.Add(newItem);
                    db.SaveChanges();
                    message.InnerHtml = "Thêm hình mới thành công";
                    message.Visible   = true;
                    LoadData();
                }
                catch (Exception)
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Có lỗi')", true);
                    return;
                }
            }
        }
        else
        {
            var query = (from d in db.HinhAnhHoatDongs
                         where d.HinhAnhHoatDongID == ID
                         select d).FirstOrDefault();
            query.Name_Ext = Link;
            query.Show     = Show;
            try
            {
                db.SaveChanges();
                Response.Redirect("/admin/HinhAnhHoatDong.aspx");
            }
            catch (Exception)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Có lỗi!')", true);
                return;
            }
        }
    }