public ActionResult Create(News news)
        {
            if (ModelState.IsValid)
            {
                db.News.Add(news);
                db.SaveChanges();
                if (news.LogoFile != null)
                {
                    var pic      = string.Empty;
                    var folder   = "~/Photos";
                    var file     = string.Format("{0}.png", news.NewsId);
                    var response = FilesHelper.UploadPhoto(news.LogoFile, folder, file);
                    if (response)
                    {
                        pic        = string.Format("{0}/{1}", folder, file);
                        news.Photo = pic;
                    }
                }

                db.Entry(news).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "Name", news.CategoryId);
            return(View(news));
        }
예제 #2
0
        public IHttpActionResult PutNews(int id, News news)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != news.NewsId)
            {
                return(BadRequest());
            }

            db.Entry(news).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!NewsExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
예제 #3
0
 public ActionResult Edit([Bind(Include = "CategoryId,Name")] Category category)
 {
     if (ModelState.IsValid)
     {
         db.Entry(category).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(category));
 }