Csharp 14
Csharp 14
Csharp 14
employee.
In our previous videos in this series, we already implemented Edit razor page. The
HTML markup and the code required to implement a Create razor page is very similar
to an Edit razor page.
Instead of creating a separate Create razor page and duplicating the HTML and code,
we are going to modify the code in Edit razor page so it can be used for both the
workflows i.e
Adding a new employee
Editing an existing employee
IEmployeeRepository.cs
MockEmployeeRepository.cs
Provide implementation for Add() method. At the moment, we are still working with
in-memory data. So we have to manually compute the ID value of the new employee
being added.
When we add support for SQL server, we do no have to manually compute the employee
id value. This will be automatically provided by SQL server when a new record is
inserted. We will see this in action in our upcoming videos.
public MockEmployeeRepository()
{
_employeeList = new List<Employee>()
{
new Employee() { Id = 1, Name = "Mary", Department = Dept.HR,
Email = "mary@pragimtech.com", PhotoPath="mary.png" },
new Employee() { Id = 2, Name = "John", Department = Dept.IT,
Email = "john@pragimtech.com", PhotoPath="john.png" },
new Employee() { Id = 3, Name = "Sara", Department = Dept.IT,
Email = "sara@pragimtech.com", PhotoPath="sara.png" },
new Employee() { Id = 4, Name = "David", Department = Dept.Payroll,
Email = "david@pragimtech.com" },
};
}
if (employee != null)
{
employee.Name = updatedEmployee.Name;
employee.Email = updatedEmployee.Email;
employee.Department = updatedEmployee.Department;
employee.PhotoPath = updatedEmployee.PhotoPath;
}
return employee;
}
_Layout.cshtml
The navigation menu is in the Layout view. Include the following HTML for the Add
menu item.
<li class="nav-item">
<a class="nav-link text-dark" asp-area=""
asp-page="/Employees/Edit">Add</a>
</li>
Edit.cshtml
<hr />
@section Scripts {
<script>
$(document).ready(function () {
$('.custom-file-input').on("change", function () {
var fileName = $(this).val().split("\\").pop();
$(this).next('.custom-file-label').html(fileName);
});
});
</script>
}
</form>
Edit.cshtml.cs
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using RazorPagesTutorial.Models;
using RazorPagesTutorial.Services;
using System;
using System.IO;
namespace RazorPagesTutorial.Pages.Employees
{
public class EditModel : PageModel
{
private readonly IEmployeeRepository employeeRepository;
private readonly IWebHostEnvironment webHostEnvironment;
[BindProperty]
public Employee Employee { get; set; }
[BindProperty]
public IFormFile Photo { get; set; }
[BindProperty]
public bool Notify { get; set; }
public string Message { get; set; }
if (Employee == null)
{
return RedirectToPage("/NotFound");
}
return Page();
}
Employee.PhotoPath = ProcessUploadedFile();
}
return Page();
}
TempData["message"] = Message;
if (Photo != null)
{
string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath,
"images");
uniqueFileName = Guid.NewGuid().ToString() + "_" + Photo.FileName;
string filePath = Path.Combine(uploadsFolder, uniqueFileName);
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
Photo.CopyTo(fileStream);
}
}
return uniqueFileName;
}
}
}
Employee.cs
Decorate Department property in the Employee class with the [Required] attribute.
Edit.cshtml