Students 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 Students 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. 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 array asynchronously. And uses the Lambda method to briefly show the anonymous method. And then saves it to the database after deleting the selected object. Details lets us view more detailed information about the given object. It also uses the FirstorDefaultAsync and Lambda methods. Edit lets us modify the given object. It uses the FirstorDefaultAsync and Lambda methods. It also has a post method which then saves everything that was changed. Clone allows us to make a copy of a given object. It uses FirstorDefaultAsync and a Lambda method to pass information to the view and save it after the copy is made.

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

namespace TallinnaRakenduslikKolledzKaur.Controllers
{
    public class StudentsController : Controller
    {
        private readonly SchoolContext _context;
        public StudentsController(SchoolContext context) 
        {
            _context = context;
        }
        public async Task<IActionResult> Index()
        {
            return View(await _context.Students.ToListAsync());
        }
        [HttpGet]
        public IActionResult Create()
        {
            return View();
        }
        /// <summary>
        ///  Lisa uus student
        /// </summary>
        /// <param name="student"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id, LastName,FirstName,EnrollmentDate,Commendments")] Student student)
        {
            if (ModelState.IsValid)
            {
                _context.Students.Add(student);
                await _context.SaveChangesAsync();
                return RedirectToAction("Index");
                // return RedirectToAction(nameof(Index))
            }
            return View(student);
        }
        /// <summary>
        ///  Get delete view for student
        /// </summary>
        /// <param name="Id">id of student</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Delete(int? Id)
        {
            if (Id == null)
            {
                return NotFound();
            }
            var student = await _context.Students.FirstOrDefaultAsync(m => m.Id == Id);
            if (student == null) 
            {
                return NotFound();
            }
            return View(student);
        }
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int? Id)
        {
            var student = await _context.Students.FindAsync(Id);
            _context.Students.Remove(student);
            await _context.SaveChangesAsync();
            return RedirectToAction("Index");
        }
        [HttpGet]
        public async Task<IActionResult> Details(int? Id)
        {
            if (Id == null)
            {
                return NotFound();
            }
            var student = await _context.Students.FirstOrDefaultAsync(m => m.Id == Id);
            if (student == null)
            {
                return NotFound();
            }
            return View(student);
        }
                     [HttpGet]
        public async Task<IActionResult>Edit(int? Id)
        {
            if (Id == null)
            {
                return NotFound();
            }
            var student = await _context.Students.FirstOrDefaultAsync(m => m.Id == Id);
            if (student == null)
            {
                return NotFound();
            }
            _context.Students.Update(student);
            return View(student);
        }
        [HttpPost, ActionName("Edit")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> EditConfirmed([Bind("Id,LastName,FirstName,EnrollmentDate,Commendments")] Student student)
        {
            _context.Students.Update(student);
            await _context.SaveChangesAsync();
            return RedirectToAction("Index");
        }
        [HttpGet]
        public async Task<IActionResult> Clone(int? Id)
        {
            if (Id == null)
            {
                return NotFound();
            }
            var student = await _context.Students.FirstOrDefaultAsync(m => m.Id == Id);
            if (student == null)
            {
                return NotFound();
            }
            return View(student);
        }
        [HttpPost, ActionName("Clone")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> CloneConfirmed(int? Id)
        {
            var student = await _context.Students.FirstOrDefaultAsync(m => m.Id == Id);
            //ModelState.Remove("Id");
            if (ModelState.IsValid)
            {
                _context.Students.Add(student);
                await _context.SaveChangesAsync();
                return RedirectToAction("Index");
                // return RedirectToAction(nameof(Index))
            }
            return View(student);
        }
    }
}

Mudel

using System.ComponentModel.DataAnnotations;

namespace TallinnaRakenduslikKolledzKaur.Models
{
    public class Student
    {
        [Key]
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public ICollection<Enrollment>? Enrollments { get; set; }
        public DateTime EnrollmentDate { get; set; }

        public int? GPA { get; set; }
        public ICollection<Commendation>? Commendations { get; set; } /* Kiitused */
        public ICollection<Mark>? Marks { get; set; } /* Märkused */



    }
}

View

Clone

@model TallinnaRakenduslikKolledzKaur.Models.Student

@{
	ViewData["Title"] = "Copy õpilane";
}

<h1>Copy õpilane</h1>
<hr />
<div>
	<h4>Õpilane @Model.FirstName @Model.LastName</h4>
	<hr />
	<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.LastName)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.LastName)</dd>
	</dl>
	<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.FirstName)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.FirstName)</dd>
	</dl>
	<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.EnrollmentDate)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.EnrollmentDate)</dd>
	</dl>
	<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.Commendations)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.Commendations)</dd>
	</dl>

	<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.Enrollments)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.Enrollments)</dd>
	</dl>


	<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.Marks)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.Marks)</dd>
	</dl>

</div>
<form asp-action="Clone">
	<input type="hidden" asp-for="Id" />
	<input type="submit" asp-route-id="Id"value="Clone" class="btn btn-primary" /> | <a asp-action="Index">Tühista</a>
</form>

Create

@model TallinnaRakenduslikKolledzKaur.Models.Student

@{
	ViewData["Title"] = "Loo uus õpilane";
}

<h1>Loo uus õpilane</h1>
<hr/>
<div class="row">
	<div class="col-md-4">
		<form asp-action="Create">

			<div asp-validation-summary="ModelOnly" class="text-danger"></div>

			<div class="form-group">
				<label asp-for="LastName" class="control-label"></label>
				<input asp-for="LastName" class="form-control" />
				<span asp-validation-for="LastName" class="text-danger"></span>
			</div>
			<div class="form-group">
				<label asp-for="FirstName" class="control-label"></label>
				<input asp-for="FirstName" class="form-control" />
				<span asp-validation-for="FirstName" class="text-danger"></span>
			</div>
			<div class="form-group">
				<label asp-for="EnrollmentDate" class="control-label"></label>
				<input asp-for="EnrollmentDate" class="form-control" />
				<span asp-validation-for="EnrollmentDate" class="text-danger"></span>
			</div>
			<div class="form-group">
				<label asp-for="Commendations" class="control-label"></label>
				<input asp-for="Commendations" class="form-control" />
				<span asp-validation-for="Commendations" class="text-danger"></span>
			</div>
			<div class="form-group">
				<input type="submit" value="Sisesta" class="btn btn-primary"/>
			</div>
		</form>
	</div>
</div>
		
<div>
	<a asp-action="Index">Mine tagasi loendisse</a>
</div>

Delete

@model TallinnaRakenduslikKolledzKaur.Models.Student

@{
	ViewData["Title"] = "Kustuta õpilane";
}

<h1>Kustuta õpilane</h1>
<h3>
	Kas oled kindel et tahad õpilast @Model.FirstName @Model.LastName eemaldada?
</h3>
<div>
	<h4>Õpilane @Model.FirstName @Model.LastName</h4>
	<hr />
	<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.LastName)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.LastName)</dd>
	</dl>
	<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.FirstName)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.FirstName)</dd>
	</dl>
	<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.EnrollmentDate)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.EnrollmentDate)</dd>
	</dl>
	<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.Commendations)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.Commendations)</dd>
	</dl>


	@if (Model.Enrollments != null)
	{
		foreach (var Enrollments in Model.Enrollments)
		{
			<dl class="row">
				<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.Enrollments)</dt>
				<dd class="col-sm-10">@Html.DisplayFor(Model => Model.Enrollments)</dd>
			</dl>
		}
	}
	@if (Model.Marks != null)
	{
		foreach (var Marks in Model.Marks)
		{
			<dl class="row">
				<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.Marks)</dt>
				<dd class="col-sm-10">@Html.DisplayFor(Model => Model.Marks)</dd>
			</dl>
		}
	}
</div>

<form asp-action="Delete">
	<input type="hidden" asp-for="Id"/>
	<input type="submit" value="Kustuta õpilane" class="btn btn-danger"/> | <a asp-action="Index">Tühista</a>
</form>

Details

@model TallinnaRakenduslikKolledzKaur.Models.Student

@{
	ViewData["Title"] = "Viewing õpilane";
}

<div>
	<h4>Õpilane @Model.FirstName @Model.LastName</h4>
	<hr />
	<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.LastName)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.LastName)</dd>
	</dl>
	<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.FirstName)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.FirstName)</dd>
	</dl>
	<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.EnrollmentDate)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.EnrollmentDate)</dd>
	</dl>
	<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.Commendations)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.Commendations)</dd>
	</dl>

	<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.Enrollments)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.Enrollments)</dd>
	</dl>


	<dl class="row">
		<dt class="col-sm-2">@Html.DisplayNameFor(Model => Model.Marks)</dt>
		<dd class="col-sm-10">@Html.DisplayFor(Model => Model.Marks)</dd>
	</dl>

</div>


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

Edit

@model TallinnaRakenduslikKolledzKaur.Models.Student

@{
	ViewData["Title"] = "Muuda õpilane";
}

<h1>Muuda õpilane</h1>
<hr />
<div class="row">
	<div class="col-md-4">
		<form asp-action="Edit">

			<div asp-validation-summary="ModelOnly" class="text-danger"></div>

			<div class="form-group">
				<label asp-for="LastName" class="control-label"></label>
				<input asp-for="LastName" class="form-control" />
				<span asp-validation-for="LastName" class="text-danger"></span>
			</div>
			<div class="form-group">
				<label asp-for="FirstName" class="control-label"></label>
				<input asp-for="FirstName" class="form-control" />
				<span asp-validation-for="FirstName" class="text-danger"></span>
			</div>
			<div class="form-group">
				<label asp-for="EnrollmentDate" class="control-label"></label>
				<input asp-for="EnrollmentDate" class="form-control" />
				<span asp-validation-for="EnrollmentDate" class="text-danger"></span>
			</div>
			<div class="form-group">
				<label asp-for="Commendations" class="control-label"></label>
				<input asp-for="Commendations" class="form-control" />
				<span asp-validation-for="Commendations" class="text-danger"></span>
			</div>
			<div class="form-group">
				<label asp-for="Marks" class="control-label"></label>
				<input asp-for="Marks" class="form-control" />
				<span asp-validation-for="Marks" class="text-danger"></span>
			</div>
			<div class="form-group">
				<label asp-for="Enrollments" class="control-label"></label>
				<input asp-for="Enrollments" class="form-control" />
				<span asp-validation-for="Enrollments" class="text-danger"></span>
			</div>
			<div class="form-group">
				<label asp-for="GPA" class="control-label"></label>
				<input asp-for="GPA" class="form-control" />
				<span asp-validation-for="GPA" class="text-danger"></span>
			</div>
			<div class="form-group">
				<input type="submit" value="Edit" class="btn btn-primary" />
			</div>
		</form>
	</div>
</div>

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

Index

@model IEnumerable<TallinnaRakenduslikKolledzKaur.Models.Student>
@{
	ViewData["Title"] = "Õpilased";
}
<h1>Õpilaste Loend:</h1>
<p>
	<a asp-action="Create">Sisesta Õpilane</a>
</p>
<table class="table">
	<thead>
		<tr>
			<th>
				@Html.DisplayNameFor(Model => Model.LastName)
			</th>
			<th>
				@Html.DisplayNameFor(Model => Model.FirstName)
			</th>
			<th>
				@Html.DisplayNameFor(Model => Model.EnrollmentDate)
			</th>
			<th>
				@Html.DisplayNameFor(Model => Model.GPA)
			</th>
			<th>Tööriistad</th>
		</tr>
	</thead>
	<tbody>
		@foreach (var student in Model)
		{
			<tr>
				<td>
					@Html.DisplayFor(modelItem => student.LastName)
				</td>
				<td>
					@Html.DisplayFor(modelItem => student.FirstName)
				</td>
				<td>
					@Html.DisplayFor(modelItem => student.EnrollmentDate)
				</td>
				<td>
					@Html.DisplayFor(modelItem => student.GPA)
				</td>
				<td>
					<a asp-action="Edit" asp-route-id="@student.Id">Muuda</a>
				</td>
				<td>
					<a asp-action="Details" asp-route-id="@student.Id">Vaata</a>
				</td>
				<td>
					<a asp-action="Delete" asp-route-id="@student.Id">Kustuta</a>
				</td>
				<!--<td>
					<a asp-action="Clone" asp-route-id="@student.Id">Clone</a>
				</td>  -->
			</tr>
		}
	</tbody>
</table>