Exemplo n.º 1
0
        public ActionResult Create(ArticleViewModels item, HttpPostedFileBase urlImage)
        {
            article articlex = new article();

            //Import Image
            if (urlImage == null || urlImage.ContentLength == 0)
            {
                ViewBag.disabled = true;
                ViewBag.Error    = "Please select a valid image ";
                return(View("Create"));
            }//end if
            else
            {
                if (ModelState.IsValid && urlImage.FileName.ToLower().EndsWith("jpg") || urlImage.FileName.ToLower().EndsWith("png") || urlImage.FileName.ToLower().EndsWith("jepg"))
                {
                    string pathImage = Server.MapPath("~/Content/" + urlImage.FileName);
                    if (System.IO.File.Exists(pathImage))
                    {
                        System.IO.File.Delete(pathImage);
                    }//end if
                    urlImage.SaveAs(pathImage);

                    articlex.body      = item.Body;
                    articlex.name      = item.Name;
                    articlex.image     = urlImage.FileName;
                    articlex.date      = DateTime.Now;;
                    articlex.favourite = false;
                    articlex.other     = "v";
                    articlex.user      = null;

                    /*Using Web Service*/

                    //HttpClient Client = new HttpClient();
                    //Client.BaseAddress = new Uri("http://localhost:18080/");
                    //Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    ////HttpResponseMessage response = Client.GetAsync("pidev-web/rest/article").Result;
                    //Client.PostAsJsonAsync<article>("pidev-web/rest/article", articlex).ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode());

                    /*Using DbContext*/
                    //ctx.article.Add(articlex);
                    //ctx.SaveChanges();
                    /*Using designe pattern*/
                    serviceArticle.Add(articlex);
                    serviceArticle.Commit();

                    return(RedirectToAction("index"));
                }//end if
                else
                {
                    ViewBag.disabled = true;
                    ViewBag.Error    = "File type is not valid";
                    return(RedirectToAction("Create"));
                }
            }//end else
        }