public ActionResult Create(int ShopProductID)
        {
            ShopProductAndProductImageModel model = new ShopProductAndProductImageModel();

            List <ShopProductAndProductImageModel> ShopProductInfo = new List <ShopProductAndProductImageModel>();
            List <Images> imgList  = new List <Images>();
            var           readTask = "";

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(WebAPIUrl);
                var responseTask = client.GetAsync("/api/GetShopProductImageByShopProductID?ShopProductID=" + ShopProductID);
                responseTask.Wait();

                var result = responseTask.Result;
                if (result.IsSuccessStatusCode)
                {
                    readTask        = result.Content.ReadAsStringAsync().Result;
                    ShopProductInfo = JsonConvert.DeserializeObject <List <ShopProductAndProductImageModel> >(readTask);
                }

                foreach (var images in ShopProductInfo)
                {
                    Images image = new Images();
                    image.Name = images.ImageName;
                    imgList.Add(image);
                }
                model.ImageList = imgList;
            }
            return(View(model));
        }
        public ActionResult Create(int ID, int ShopID)
        {
            ShopProductAndProductImageModel model = new ShopProductAndProductImageModel();

            if (ID != 0)
            {
                var response = client.GetAsync(string.Format("{0}/api/ShopProducts/{1}", WebAPIUrl, ID)).Result;
                if (response.IsSuccessStatusCode)
                {
                    var    responseString = JObject.Parse(response.Content.ReadAsStringAsync().Result);
                    string code           = responseString.SelectToken("code").ToString();
                    if (code == "0")
                    {
                        model.ID          = (int)responseString["data"]["ID"];
                        model.Name        = (string)responseString["data"]["Name"];
                        model.Description = (string)responseString["data"]["Description"];
                        model.Discount    = (decimal)responseString["data"]["Discount"];
                        model.Price       = (decimal)responseString["data"]["Price"];
                        model.ShopID      = (int)responseString["data"]["ShopID"];
                        model.Active      = Convert.ToBoolean(responseString["data"]["Active"]);
                    }
                    else
                    {
                    }
                }
                return(View(model));
            }
            else
            {
                return(View(model));
            }
        }
        public ActionResult Create(ShopProductAndProductImageModel model, List <HttpPostedFileBase> postedFiles)
        {
            List <Images> imgList = new List <Images>();

            foreach (HttpPostedFileBase postedFile in postedFiles)
            {
                Images image = new Images();
                if (postedFile != null)
                {
                    //Extract Image File Name.
                    string ImageName = System.IO.Path.GetFileName(postedFile.FileName);

                    //Set the Image File Path.
                    string ImagePath = "~/Areas/ProductImages/" + ImageName;

                    model.ShopProductID = model.ShopProductID;
                    model.ImageName     = ImageName;
                    model.ImagePath     = ImagePath;
                    model.Active        = true;
                    model.Created       = System.DateTime.Now;
                    model.CreatedBy     = 1;
                    model.Updated       = System.DateTime.Now;
                    model.UpdatedBy     = 1;

                    string data     = Newtonsoft.Json.JsonConvert.SerializeObject(model);
                    var    content  = new StringContent(data, Encoding.UTF8, "application/json");
                    var    response = client.PostAsync(string.Format("{0}/api/ShopProductImages", WebAPIUrl), content).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        var    responseString = JObject.Parse(response.Content.ReadAsStringAsync().Result);
                        string code           = responseString.SelectToken("code").ToString();
                        if (code == "0")
                        {
                            //Save the Image File in Folder.
                            postedFile.SaveAs(Server.MapPath(ImagePath));
                        }
                    }

                    //string fileName = Path.GetFileName(postedFile.FileName);
                    //postedFile.SaveAs(path + fileName);

                    image.Name = ImageName;
                    imgList.Add(image);

                    model.ImageList = imgList;

                    ViewBag.Message += string.Format("<b>{0}</b> uploaded.<br />", ImageName);
                }
            }

            List <ShopProductAndProductImageModel> ShopProductInfo = new List <ShopProductAndProductImageModel>();
            var readTask = "";

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(WebAPIUrl);
                var responseTask = client.GetAsync("/api/GetShopProductImageByShopProductID?ShopProductID=" + model.ShopProductID);
                responseTask.Wait();

                var result = responseTask.Result;
                if (result.IsSuccessStatusCode)
                {
                    readTask        = result.Content.ReadAsStringAsync().Result;
                    ShopProductInfo = JsonConvert.DeserializeObject <List <ShopProductAndProductImageModel> >(readTask);
                }

                foreach (var images in ShopProductInfo)
                {
                    Images image = new Images();
                    image.Name = images.ImageName;
                    imgList.Add(image);
                }
                model.ImageList = imgList;
            }
            return(View(model));
        }
        public ActionResult Create(ShopProductAndProductImageModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (model.ID > 0)
                    {
                        model.Active    = model.Active;
                        model.Created   = System.DateTime.Now;
                        model.CreatedBy = 1;
                        model.Updated   = System.DateTime.Now;
                        model.UpdatedBy = 1;

                        string data     = Newtonsoft.Json.JsonConvert.SerializeObject(model);
                        var    content  = new StringContent(data, Encoding.UTF8, "application/json");
                        var    response = client.PutAsync(string.Format("{0}/api/ShopProducts", WebAPIUrl), content).Result;
                        if (response.IsSuccessStatusCode)
                        {
                            var    responseString = JObject.Parse(response.Content.ReadAsStringAsync().Result);
                            string code           = responseString.SelectToken("code").ToString();
                            if (code == "0")
                            {
                                return(RedirectToAction("Index", "ShopProduct", new { ShopID = model.ShopID }));
                            }
                            else
                            {
                                ViewBag.Message = "Product not found";
                                return(View(model));
                            }
                        }
                        else
                        {
                            ViewBag.Message = "Update failed.";
                            return(View(model));
                        }
                    }
                    else
                    {
                        model.Active    = true;
                        model.Created   = System.DateTime.Now;
                        model.CreatedBy = 1;
                        model.Updated   = System.DateTime.Now;
                        model.UpdatedBy = 1;

                        string data     = Newtonsoft.Json.JsonConvert.SerializeObject(model);
                        var    content  = new StringContent(data, Encoding.UTF8, "application/json");
                        var    response = client.PostAsync(string.Format("{0}/api/ShopProducts", WebAPIUrl), content).Result;
                        if (response.IsSuccessStatusCode)
                        {
                            var    responseString = JObject.Parse(response.Content.ReadAsStringAsync().Result);
                            string code           = responseString.SelectToken("code").ToString();
                            if (code == "0")
                            {
                                return(RedirectToAction("Index", "ShopProduct", new { ShopID = model.ShopID }));
                            }
                        }
                        else
                        {
                            ViewBag.Message = "Insert failed. " + response.ToString();
                            return(View(model));
                        }
                    }
                }
                return(View(model));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }