Beispiel #1
0
        public JsonResult InsertNewCountry()
        {
            var country = new CountryViewModel();

            try
            {
                var resolveRequest = HttpContext.Request;
                resolveRequest.InputStream.Seek(0, System.IO.SeekOrigin.Begin);
                string jsonString = new System.IO.StreamReader(resolveRequest.InputStream).ReadToEnd();
                //deserialse
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                string countryname = serializer.Deserialize <string>(jsonString);
                using (CountryViewModel presentaion = new CountryViewModel())
                {
                    country.CountryName = countryname;
                    var retVal = presentaion.Insert(country);;
                    if (retVal >= 1)
                    {
                        //get the newly artist and show

                        country = presentaion.GetObject(retVal);

                        return(new JsonResult
                        {
                            Data = new { Data = country, Success = true, ErrorMessage = "Country was created" },
                            ContentEncoding = System.Text.Encoding.UTF8,
                            JsonRequestBehavior = JsonRequestBehavior.AllowGet
                        });
                    }
                    else
                    {
                        return(new JsonResult
                        {
                            Data = new { Data = country, Success = false, ErrorMessage = "Error in creating Country" },
                            ContentEncoding = System.Text.Encoding.UTF8,
                            JsonRequestBehavior = JsonRequestBehavior.AllowGet
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                return(new JsonResult
                {
                    Data = new { Data = country, Success = false, ErrorMessage = ex.Message },
                    ContentEncoding = System.Text.Encoding.UTF8,
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }