public async Task<IHttpActionResult> PutModel(int id, ModelDTO model) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != model.ModelId) { return BadRequest(); } Model updateModel = new Model(); updateModel.ModelId = model.ModelId; updateModel.Name = model.ModelName; updateModel.ManufacturerCode = model.ManufacturerCode; updateModel.CategoryId = Int32.Parse(model.CategoryId); updateModel.Description = model.Description; updateModel.StatusId = Int32.Parse(model.StatusId); updateModel.ManufacturerId = Int32.Parse(model.ManufacturerId); updateModel.ListPrice = model.ListPrice; db.Models.Attach(updateModel); db.Entry(updateModel).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ModelExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public async Task<IHttpActionResult> PostModel(ModelDTO model) { /* todo - how to not have this trip up on a missing modelId... // ModelState.Remove("modelId"); doesn't work, state is already invalid at this point if (!ModelState.IsValid) { return BadRequest(ModelState); }*/ Model updateModel = new Model(); updateModel.Name = model.ModelName; updateModel.ManufacturerCode = model.ManufacturerCode; updateModel.CategoryId = Int32.Parse(model.CategoryId); updateModel.Description = model.Description; updateModel.StatusId = Int32.Parse(model.StatusId); updateModel.ManufacturerId = Int32.Parse(model.ManufacturerId); updateModel.ListPrice = model.ListPrice; db.Models.Add(updateModel); await db.SaveChangesAsync(); return CreatedAtRoute("DefaultApi", new { id = model.ModelId }, model); }