public IHttpActionResult PostAddPerson(Person newPerson)
        {
            using (PeopleDBEntities context = new PeopleDBEntities())
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest());
                }

                var personObj = context.People.FirstOrDefault(p => p.id == newPerson.id);
                if (personObj != null)
                {
                    personObj.fname     = newPerson.fname;
                    personObj.lname     = newPerson.lname;
                    personObj.age       = newPerson.age;
                    personObj.addr      = newPerson.addr;
                    personObj.interests = newPerson.interests;
                    personObj.picture   = newPerson.picture;
                }
                else
                {
                    context.People.Add(newPerson);
                }

                context.SaveChanges();

                return(Ok());
            }
        }
Beispiel #2
0
        public IHttpActionResult Post()
        {
            var    httpRequest   = HttpContext.Current.Request;
            string imageName     = null;
            string FirstNameeeee = httpRequest["FirstName"];

            //upload image
            var postedFile = httpRequest.Files["Picture"];

            if (postedFile != null && postedFile.ContentLength > 0)
            {
                //create custom filename
                imageName = new string(Path.GetFileNameWithoutExtension(postedFile.FileName).Take(10).ToArray()).Replace(" ", "-");
                imageName = imageName + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(postedFile.FileName);
                var filePath = HttpContext.Current.Server.MapPath("~/Upload/" + imageName);
                postedFile.SaveAs(filePath);
            }
            else
            {
                return(Json(new { StatusCode = "Error occured!" }));
            }

            try
            {
                var pu = new person
                {
                    FirstName = httpRequest["FirstName"],
                    LastName  = httpRequest["LastName"],
                    Address   = httpRequest["Address"],
                    Age       = Convert.ToInt16(httpRequest["Age"]),
                    Interests = httpRequest["Interests"],
                    Picture   = imageName
                };
                db.people.Add(pu);
                db.SaveChanges();
                return(Json(new { StatusCode = "Added Successfully!" }));
            }
            catch (Exception ex)
            {
                return(BadRequest());
            }
        }
        public ActionResult Create(People people)
        {
            if (ModelState.IsValid)
            {
                if (people.PersonName.Contains("فحش"))
                {
                    ModelState.AddModelError("", "استفاده از این کلمه مجاز نمی باشد");
                    return(View(people));
                }

                db.People.Add(people);
                db.SaveChanges();
                return(RedirectToAction(nameof(Index)));
            }

            return(View(people));
        }