public IActionResult LampControl(LampViewModel model) { switch (model.All) { case true: switch (model.controltype) { case "Frequency": systemControl.ChangeFrequency(model.Frequency); break; case "Dim": systemControl.Dim(model.DutyCycle); break; } break; case false: switch (model.controltype) { case "Frequency": systemControl.ChangeFrequency(model.Index, model.Frequency); break; case "Dim": systemControl.Dim(model.Index, model.DutyCycle); break; } break; } return(View()); }
[ValidateAntiForgeryToken] //To prevent handling Post requests from the foreign apps public async Task <IActionResult> Create(LampViewModel lamp) { if (!ModelState.IsValid) { return(View(lamp)); //There are some validation errors, reshow form } //Here could be used AutoMapper or something like this... db.Lamps.Add(new Lamp { //lamp.Id is ignored here Cost = lamp.Cost, ImageRef = lamp.ImageRef, LampType = lamp.LampType, Manufacturer = lamp.Manufacturer }); await db.SaveChangesAsync(); //DB/ORM exceptions probably should being handled in a special way... return(RedirectToAction("Products")); }
public async Task <IActionResult> Edit(LampViewModel lamp) { if (!ModelState.IsValid) { return(View(lamp)); //There are some validation errors, reshow form } Lamp item = await db.Lamps.FirstOrDefaultAsync(o => o.Id == lamp.Id); if (item is null) { return(BadRequest($"Item with id = {lamp.Id} was not found.")); } item.Cost = lamp.Cost; item.ImageRef = lamp.ImageRef; item.LampType = lamp.LampType; item.Manufacturer = lamp.Manufacturer; db.Lamps.Update(item); await db.SaveChangesAsync(); //DB/ORM exceptions probably should being handled in a special way... return(RedirectToAction("Products")); }