Departments Controller, Model ja View

Controller

The SchoolContext _context tells the Controller and View what the database looks like and all the information inside it.

Index lets us view all the Departments information in general using the ToListAsync() method which puts all the books into a table.

Create first gives us the Create view and then once the information is given, it checks that the model is correct and if so, adds it to the database and takes you back to the Index.
In this case it also tells the ViewBag that “Creation” is True so that the view will show the Creation view.
It also uses SaveChangesAsync to save the information

Delete deletes the given selection after checking that it exists. It first uses FirstorDefaultAsync which returns the first element of the sequence asynchronously. And uses the Lambda method to briefly show the anonymous method.
In this case it also tells the ViewBag that “Deletion” is True so that the view knows what to show.
And then saves it to the database when it deletes the selected object.
Details allows us to view more detailed information about a given object. It also uses FirstorDefaultAsync and the Lambda method. It also tells ViewBag that deletion is false so that the view shows Details. Edit allows us to modify a given object. It uses FirstorDefaultAsync and the Lambda method. It also has a post method which then saves everything that was changed. It also tells ViewBag that Creation is false so that it knows which view to show.

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using TallinnaRakenduslikKolledzKaur.Data;
using TallinnaRakenduslikKolledzKaur.Models;

namespace TallinnaRakenduslikKolledzKaur.Controllers
{
    public class DepartmentsController : Controller
    {
        private readonly SchoolContext _context;
        public DepartmentsController(SchoolContext context)
        {
            _context = context;
        }
        public async Task<IActionResult> Index()
        {
            var schoolContext = _context.Departments.Include(d => d.Administrator);
            return View(await schoolContext.ToListAsync());
        }

        [HttpGet]
        public IActionResult Create()
        {
            ViewData["creation"] = true;
            ViewData["InstructorID"] = new SelectList(_context.Instructors,"Id", "FullName");
            /*ViewData["StudentId"] = new SelectList(_context.Students, "Id", "LastName", "FirstName");*/
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Name,Budget,StartDate,RowVersion,InstructorId,Staplers,WastedHours")]Department department)
        {
            if (ModelState.IsValid)
            {
                _context.Add(department);
                await _context.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            ViewData["InstructorID"] = new SelectList(_context.Instructors, "Id", "FullName", department.InstructorID);
            /*ViewData["CourseStatus"] = new SelectList(_context.Instructors,"Id",department.CurrentStatus.ToString)*/
            return View(department);
        }
        [HttpGet]
        public async Task<IActionResult> Edit(int? id)
        {
            ViewData["creation"] = false;
            if (id == null)
            {
                return NotFound();
            }
            var department = await _context.Departments.FirstOrDefaultAsync(d => d.DepartmentID == id);
            if (department == null)
            {
                return NotFound();
            }
            /*_context.Departments.Update(department);        */
            return View("Create", department);
        }
        [HttpPost, ActionName("Edit")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> EditConfirmed([Bind("DepartmentID,Name,Budget,StartDate,RowVersion,InstructorId,Staplers,WastedHours")] Department department)
        {
            _context.Departments.Update(department);
            await _context.SaveChangesAsync();
            return RedirectToAction("Index");
        }
        [HttpGet]
        public async Task<IActionResult> Delete(int? id)
        {
            ViewData["deletion"] = true;
            if (id == null)
            {
                return NotFound();
            }
            var department = await _context.Departments.FirstOrDefaultAsync(d => d.DepartmentID == id);
            if (department == null)
            {
                return NotFound();
            }
            return View(department);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Delete(Department department)
        {
            if (await _context.Departments.AnyAsync(m => m.DepartmentID == department.DepartmentID))
            {
                _context.Departments.Remove(department);
                await _context.SaveChangesAsync();
            }
            return RedirectToAction("Index");
        }

        [HttpGet]
        public async Task<IActionResult> Details(int? id)
        {
            ViewData["deletion"] = false;
            if (id == null)
            {
                return NotFound();
            }
            var department = await _context.Departments.FirstOrDefaultAsync(d => d.DepartmentID == id);
            if (department == null)
            {
                return NotFound();
            }
            return View("Delete", department);
        }
    }

}

Mudel

using System.ComponentModel.DataAnnotations;

namespace TallinnaRakenduslikKolledzKaur.Models
{
    public class Department
    {
        [Key]
        public int DepartmentID { get; set; }
        public string Name { get; set; }
        public decimal Budget { get; set; }
        public DateTime StartDate { get; set; }
        public int? InstructorID { get; set; }
        public Instructor? Administrator { get; set; }
        public ICollection<Course>? Courses { get; set; }
        public byte? RowVersion { get; set; }
        public int? Staplers { get; set; }
        public string? Accomplishments { get; set; }
        public int? WastedHours { get; set; }
    }
}

View

Create + Edit

@model TallinnaRakenduslikKolledzKaur.Models.Department

@if (ViewBag.creation == true)
{
	ViewData["Title"] = "Osakond";

	<h1>Asuta uus osakond</h1>
	<h4></h4>
	<hr/>
	<div class="row">
		<div class="col-md-8">
			<form asp-action="Create">
				<input type="hidden" asp-for="RowVersion" value="@Html.Raw("0")"/>
				<div asp-validation-summary="ModelOnly" class="text-danger"></div>

				<div class="form-group">
					<label asp-for="Name" class="control-label"></label>
					<input asp-for="Name" class="control-label"/>
					<span asp-validation-for="Name" class="text-danger"></span>
				</div>

				<div class="form-group">
					<label asp-for="Budget" class="control-label"></label>
					<input asp-for="Budget" class="control-label" />
					<span asp-validation-for="Budget" class="text-danger"></span>
				</div>

				<div class="form-group">
					<label asp-for="StartDate" class="control-label"></label>
					<input asp-for="StartDate" class="control-label" />
					<span asp-validation-for="StartDate" class="text-danger"></span>
				</div>

				<div class="form-group">
					<label asp-for="Staplers" class="control-label"></label>
					<input asp-for="Staplers" class="control-label" />
					<span asp-validation-for="Staplers" class="text-danger"></span>
				</div>

				<div class="form-group">
					<label asp-for="Accomplishments" class="control-label"></label>
					<input asp-for="Accomplishments" class="control-label" />
					<span asp-validation-for="Accomplishments" class="text-danger"></span>
				</div>

				<div class="form-group">
					<label asp-for="WastedHours" class="control-label"></label>
					<input asp-for="WastedHours" class="control-label" />
					<span asp-validation-for="WastedHours" class="text-danger"></span>
				</div>

				<div class="form-group">
					<label asp-for="InstructorID" class="control-label"></label>
					<select asp-for="InstructorID" class="control-label" asp-items="ViewBag.InstructorID">
						<option value="">-- Palun Vali Õpetaja --</option>
					</select>
					<span asp-validation-for="InstructorID" class="text-danger"></span>
				</div>

				<div>
					<input type="submit" value="Sisesta" class="btn btn-primary" />
				</div>
				<div>
					<a asp-action="Index">Tühista Lisamine</a>
				</div>
			</form>
		</div>
	</div>
}

else if(ViewBag.creation == false)
{

	ViewData["Title"] = "Muuda osakond";


<h1>Muuda Department</h1>
<hr />
<div class="row">
	<div class="col-md-4">
		<form asp-action="Edit">
			<input type="hidden" asp-for="@Model.DepartmentID"	/>
			<div asp-validation-summary="ModelOnly" class="text-danger"></div>

			<div class="form-group">
				<label asp-for="Name" class="control-label"></label>
				<input asp-for="Name" class="form-control" />
				<span asp-validation-for="Name" class="text-danger"></span>
			</div>
			<div class="form-group">
				<label asp-for="StartDate" class="control-label"></label>
				<input asp-for="StartDate" class="form-control" />
				<span asp-validation-for="StartDate" class="text-danger"></span>
			</div>
			<div class="form-group">
				<label asp-for="Budget" class="control-label"></label>
				<input asp-for="Budget" class="form-control" />
				<span asp-validation-for="Budget" class="text-danger"></span>
			</div>
			<div class="form-group">
				<label asp-for="RowVersion" class="control-label"></label>
				<input asp-for="RowVersion" class="form-control" />
				<span asp-validation-for="RowVersion" class="text-danger"></span>
			</div>
			<div class="form-group">
					<label asp-for="Staplers" class="control-label"></label>
					<input asp-for="Staplers" class="form-control" />
					<span asp-validation-for="Staplers" class="text-danger"></span>
			</div>
			<div class="form-group">
					<label asp-for="Accomplishments" class="control-label"></label>
					<input asp-for="Accomplishments" class="form-control" />
					<span asp-validation-for="Accomplishments" class="text-danger"></span>
			</div>
			<div class="form-group">
					<label asp-for="WastedHours" class="control-label"></label>
					<input asp-for="WastedHours" class="form-control" />
					<span asp-validation-for="WastedHours" class="text-danger"></span>
			</div>
			<!--<div class="form-group">
					<label asp-for="InstructorID" class="control-label"></label>
					<input asp-for="InstructorID" class="form-control" />
					<span asp-validation-for="InstructorID" class="text-danger"></span>
			</div>
			<div class="form-group">
					<label asp-for="Administrator" class="control-label"></label>
					<input asp-for="Administrator" class="form-control" />
					<span asp-validation-for="Administrator" class="text-danger"></span>
			</div>		 -->
			<div class="form-group">
				<input type="submit" asp-route-id="@Model.DepartmentID"value="Edit" class="btn btn-primary" />
			</div>
		</form>
	</div>
</div>

<div>
	<a asp-action="Index">Mine tagasi loendisse</a>
</div>
}

Delete + Details

@model TallinnaRakenduslikKolledzKaur.Models.Department

@if (ViewBag.deletion == true)
{

	ViewData["Title"] = "Kustuta osakond";

<h2>@Html.DisplayFor(modelItem => Model.Name)</h2>

<p>
	<h3>Kustuta Osakond:</h3>
</p>
<div>
	<hr />
	<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.Name)</dt>
	<dt class="col-sm-2">@Html.DisplayFor(Model => Model.Name)</dt>
	<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.DepartmentID)</dt>
	<dt class="col-sm-2">@Html.DisplayFor(Model => Model.DepartmentID)</dt>
	<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.Staplers)</dt>
	<dt class="col-sm-2">@Html.DisplayFor(Model => Model.Staplers)</dt>
	<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.RowVersion)</dt>
	<dt class="col-sm-2">@Html.DisplayFor(Model => Model.RowVersion)</dt>
	<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.Budget)</dt>
	<dt class="col-sm-2">@Html.DisplayFor(Model => Model.Budget)</dt>
	<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.StartDate)</dt>
	<dt class="col-sm-2">@Html.DisplayFor(Model => Model.StartDate)</dt>
	<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.Accomplishments)</dt>
	<dt class="col-sm-2">@Html.DisplayFor(Model => Model.Accomplishments)</dt>
	<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.WastedHours)</dt>
	<dt class="col-sm-2">@Html.DisplayFor(Model => Model.WastedHours)</dt>
	<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.InstructorID)</dt>
	<dt class="col-sm-2">@Html.DisplayFor(Model => Model.InstructorID)</dt>

	<form asp-action="Delete">
		<input type="hidden" asp-for="DepartmentID" />
		<input type="hidden" asp-for="RowVersion"/>
		<input type="submit" value="Kustuta Osakond" class="btn btn-danger" /> | <a asp-action="Index">Tühista</a>
	</form>
</div>
}

else if (ViewBag.deletion == false)
{

	ViewData["Title"] = "Viewing õpetaja";


<div>
	<h4>Osakond @Model.Name</h4>
	<hr />
	<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.Name)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.Name)</dd>
	</dl>
	<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.StartDate)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.StartDate)</dd>
	</dl>
	<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.Budget)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.Budget)</dd>
	</dl>
	<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.RowVersion)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.RowVersion)</dd>
	</dl>
	<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.Staplers)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.Staplers)</dd>
	</dl>
	<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.Accomplishments)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.Accomplishments)</dd>
	</dl>
	<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.WastedHours)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.WastedHours)</dd>
	</dl>
		<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.InstructorID)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.InstructorID)</dd>
	</dl>
	<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.Administrator)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.Administrator)</dd>
	</dl>
</div>


<form asp-action="Tagasi">
	<input type="hidden" asp-for="DepartmentID" />
	<a asp-action="Index">Tagasi</a>
</form>
}

Index

@model IEnumerable<TallinnaRakenduslikKolledzKaur.Models.Department>
@{
	ViewData["Title"] = "Osakonnad";
}
<h1>Osakonnad</h1>
<p>
	<a asp-action="Create">Loo uus osakond</a>
</p>
<table class="table">
	<thead>
		<tr>
			<!--<th>@Html.DisplayNameFor(Model => Model.DepartmentID)</th>	 -->
			<th>@Html.DisplayNameFor(Model => Model.Name)</th>
			<th>@Html.DisplayNameFor(Model => Model.Budget)</th>
			<th>@Html.DisplayNameFor(Model => Model.StartDate)</th>
			<th>@Html.DisplayNameFor(Model => Model.Administrator)</th>
			<th>@Html.DisplayNameFor(Model => Model.RowVersion)</th>
			<th>@Html.DisplayNameFor(Model => Model.Accomplishments)</th>
			<th>@Html.DisplayNameFor(Model => Model.WastedHours)</th>   <!-- woag, -->
			<th>Tööriistad</th>
		</tr>
	</thead>
	<tbody>
		@foreach(var department in Model)
		{
			<tr>
				<!--<td>@Html.DisplayFor(modelItem => department.DepartmentID)</td>	  -->
				<td> @Html.DisplayFor(modelItem => department.Name) </td>
				<td> @Html.DisplayFor(modelItem => department.Budget) </td>
				<td> @Html.DisplayFor(modelItem => department.StartDate) </td>
				<td> @Html.DisplayFor(modelItem => department.Administrator) </td>
				<td> @Html.DisplayFor(modelItem => department.RowVersion) </td>
				<td> @Html.DisplayFor(modelItem => department.Accomplishments) </td>
				<td> @Html.DisplayFor(modelItem => department.WastedHours) </td>
				<td>
					@* /* Muuda */ *@
					<a asp-action="Edit" asp-route-id="@department.DepartmentID" class="btn btn-primary">Edit</a>
					@* /* Vaata */ *@
					<a asp-action="Details" asp-route-id="@department.DepartmentID" class="btn btn-primary">Detail</a>
					@* /* Vaata Admin */ *@

					@* /* Kustuta */ *@
					<a asp-action="Delete" asp-route-id="@department.DepartmentID" class="btn btn-danger">Eemalda</a>
				</td>
			</tr>
		}
	</tbody>
</table>