コード例 #1
0
 public ActionResult Create(Location location)
 {
     if (ModelState.IsValid)
     {
         locationRepository.InsertOrUpdate(location);
         locationRepository.Save();
         return RedirectToAction("Index");
     }
     else
     {
         return View();
     }
 }
コード例 #2
0
 public void InsertOrUpdate(Location location)
 {
     if (location.Id == default(int))
     {
         // New entity
         context.Locations.Add(location);
     }
     else
     {
         // Existing entity
         context.Entry(location).State = EntityState.Modified;
     }
 }
コード例 #3
0
        // POST api/LocationAPI
        public HttpResponseMessage PostLocation(Location location)
        {
            if (ModelState.IsValid)
            {
                locationRepository.InsertOrUpdate(location);
                locationRepository.Save();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, location);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = location.Id }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
コード例 #4
0
        // PUT api/LocationAPI/5
        public HttpResponseMessage PutLocation(int id, Location location)
        {
            if (ModelState.IsValid && id == location.Id)
            {
                try
                {
                    locationRepository.InsertOrUpdate(location);
                    locationRepository.Save();
                }
                catch
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK, location);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }