yavsc/Yavsc/Controllers/CommandFormsController.cs

131 lines
4.1 KiB
C#
Raw Normal View History

2017-01-23 23:52:47 +01:00
using System.Linq;
2017-02-23 03:10:30 +01:00
using System.Security.Claims;
2017-01-23 23:52:47 +01:00
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.Data.Entity;
using Yavsc.Models;
using Yavsc.Models.Workflow;
namespace Yavsc.Controllers
{
public class CommandFormsController : Controller
{
private ApplicationDbContext _context;
public CommandFormsController(ApplicationDbContext context)
{
_context = context;
}
// GET: CommandForms
public async Task<IActionResult> Index()
{
var applicationDbContext = _context.CommandForm.Include(c => c.Context);
return View(await applicationDbContext.ToListAsync());
}
// GET: CommandForms/Details/5
2017-01-24 10:43:42 +01:00
public async Task<IActionResult> Details(long? id)
2017-01-23 23:52:47 +01:00
{
if (id == null)
{
return HttpNotFound();
}
CommandForm commandForm = await _context.CommandForm.SingleAsync(m => m.Id == id);
if (commandForm == null)
{
return HttpNotFound();
}
return View(commandForm);
}
// GET: CommandForms/Create
public IActionResult Create()
{
2017-01-31 13:19:12 +01:00
SetViewBag();
2017-01-23 23:52:47 +01:00
return View();
}
2017-01-31 13:19:12 +01:00
private void SetViewBag(CommandForm commandForm=null) {
ViewBag.ActivityCode = new SelectList(_context.Activities, "Code", "Name", commandForm?.ActivityCode);
2017-03-01 10:11:20 +01:00
ViewBag.ActionName = Startup.Forms.Select( c => new SelectListItem { Value = c, Text = c, Selected = (commandForm?.ActionName == c) } );
2017-01-31 13:19:12 +01:00
}
2017-01-23 23:52:47 +01:00
// POST: CommandForms/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(CommandForm commandForm)
{
if (ModelState.IsValid)
{
_context.CommandForm.Add(commandForm);
2017-02-23 03:10:30 +01:00
await _context.SaveChangesAsync(User.GetUserId());
2017-01-23 23:52:47 +01:00
return RedirectToAction("Index");
}
2017-01-31 13:19:12 +01:00
SetViewBag(commandForm);
2017-01-23 23:52:47 +01:00
return View(commandForm);
}
// GET: CommandForms/Edit/5
2017-01-24 10:43:42 +01:00
public async Task<IActionResult> Edit(long? id)
2017-01-23 23:52:47 +01:00
{
if (id == null)
{
return HttpNotFound();
}
CommandForm commandForm = await _context.CommandForm.SingleAsync(m => m.Id == id);
if (commandForm == null)
{
return HttpNotFound();
}
2017-01-31 13:19:12 +01:00
SetViewBag(commandForm);
2017-01-23 23:52:47 +01:00
return View(commandForm);
}
// POST: CommandForms/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(CommandForm commandForm)
{
if (ModelState.IsValid)
{
_context.Update(commandForm);
2017-02-23 03:10:30 +01:00
await _context.SaveChangesAsync(User.GetUserId());
2017-01-23 23:52:47 +01:00
return RedirectToAction("Index");
}
2017-01-31 13:19:12 +01:00
SetViewBag(commandForm);
2017-01-23 23:52:47 +01:00
return View(commandForm);
}
// GET: CommandForms/Delete/5
[ActionName("Delete")]
2017-01-24 10:43:42 +01:00
public async Task<IActionResult> Delete(long? id)
2017-01-23 23:52:47 +01:00
{
if (id == null)
{
return HttpNotFound();
}
CommandForm commandForm = await _context.CommandForm.SingleAsync(m => m.Id == id);
if (commandForm == null)
{
return HttpNotFound();
}
return View(commandForm);
}
// POST: CommandForms/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
2017-01-24 10:43:42 +01:00
public async Task<IActionResult> DeleteConfirmed(long id)
2017-01-23 23:52:47 +01:00
{
CommandForm commandForm = await _context.CommandForm.SingleAsync(m => m.Id == id);
_context.CommandForm.Remove(commandForm);
2017-02-23 03:10:30 +01:00
await _context.SaveChangesAsync(User.GetUserId());
2017-01-23 23:52:47 +01:00
return RedirectToAction("Index");
}
}
}