Hi
In ASP.net Core MVC I have been given the following project where a user can upload a file. As Windows Forms VB.net developer I am trying to wrap my head around how this code is called. There are clearly no Subs and I have no idea how async works. Could someone please explain a bit or send me a link on how this works. The cshtml.cs page is shown below and the cshtml part of the page is shown further down
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using WebApplication1.Data;
using WebApplication1.Model;
namespace WebApplication1.Pages.Policies
{
public class UploadModel : PageModel
{
private readonly WebApplication1.Data.ApplicationDbContext _context;
private IHostingEnvironment _ihostingEnvironment;
public UploadModel(WebApplication1.Data.ApplicationDbContext context, IHostingEnvironment ihostingEnvironment)
{
_context = context;
_ihostingEnvironment = ihostingEnvironment;
}
public IList<UploadFile> UploadFile { get;set; }
public int PolicyID { get; set; }
public FileUpload fileUpload { get;set; }
public string Message { get; set; }
public async Task OnGetAsync(int id)
{
PolicyID = id;
UploadFile = await _context.UploadFiles.Where(m => m.PolicyID == id).ToListAsync();
}
public async Task<IActionResult> OnPost(FileUpload fileUpload, int policyID)
{
if (fileUpload.FormFiles != null && fileUpload.FormFiles.Count > 0)
{
var directoryPath = Path.Combine(_ihostingEnvironment.WebRootPath, @"Policy/" + policyID);
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
try
{
foreach (var photo in fileUpload.FormFiles)
{
var path = Path.Combine(_ihostingEnvironment.WebRootPath, @"Policy/" + policyID, photo.FileName);
var info = new FileInfo(path);
if (!System.IO.File.Exists(path))
{
var stream = new FileStream(path, FileMode.Create);
photo.CopyToAsync(stream);
var uploadFile = new UploadFile
{
PolicyID = policyID,
FilePath = photo.FileName,
Type = photo.ContentType,
UpdateAt = DateTime.Now,
};
_context.UploadFiles.Add(uploadFile);
}
}
}
catch (Exception ex)
{
Message = ex.Message.ToString();
}
await _context.SaveChangesAsync();
}
UploadFile = await _context.UploadFiles.Where(m => m.PolicyID == policyID).ToListAsync();
return RedirectToPage("Upload", new { id = policyID }); ;
}
public IActionResult OnPostDelete(int id)
{
var file = _context.UploadFiles.Find(id);
if (file == null)
{
return NotFound();
}
var path = Path.Combine(_ihostingEnvironment.WebRootPath, @"Policy/" + file.PolicyID, file.FilePath);
var info = new FileInfo(path);
if (info.Exists)
{
info.Delete();
}
_context.UploadFiles.Remove(file);
_context.SaveChanges();
UploadFile = _context.UploadFiles.Where(m => m.PolicyID == id).ToList();
return RedirectToPage("Upload", new {id = file.PolicyID });
}
}
public class FileUpload
{
public List<IFormFile> FormFiles { get; set; }
}
}
@page
@model WebApplication1.Pages.Policies.UploadModel
@{
ViewData["Title"] = "Upload";
Layout = "~/Pages/Shared/_Layout.cshtml";
}
<style>
table {
width: 100%;
}
tfoot input {
width: 100%;
padding: 3px;
box-sizing: border-box;
}
thead {
background-color: #f62d51;
color: white;
}
/*table thead th:first-child {
border-radius: 10px 0 0 0;
}*/
/*table thead th:last-child {
border-radius: 0 10px 0 0;
}*/
/* #datatable-buttons tfoot tr:last-child td:first-child {
border-radius: 0 0 0 10px;
}
#datatable-buttons tfoot tr:last-child td:last-child {
border-radius: 0 0 10px 0;
}*/
</style>
<div class="content">
<div class="row">
<div class="col-6">
<div class="page-title-box" style="margin:20px">
<a asp-page="/Index" class="btn btn-danger btn-rounded">Back List</a>
</div>
</div>
<div class="col-6">
<div class="page-title-box" style="margin:20px;">
<form method="post" enctype="multipart/form-data">
<label class="control-label">Add Another Attachment</label>
<input type="hidden" asp-for="PolicyID" />
<input type="file" asp-for="fileUpload.FormFiles" multiple />
<input type="submit" value="Upload" class="btn btn-success" />
</form>
</div>
</div>
</div>
<!-- end page title -->
<div class="row">
<div class="col-12">
<p>@Model.Message</p>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-body">
<form method="post">
<table id="scroll-horizontal-datatable" class="table w-100 nowrap">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.UploadFile[0].FilePath)
</th>
<th>
@Html.DisplayNameFor(model => model.UploadFile[0].Type)
</th>
<th>
@Html.DisplayNameFor(model => model.UploadFile[0].UpdateAt)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.UploadFile) {
<tr>
<td>
<a href="/Policy/@item.PolicyID/@item.FilePath" target="_blank">@Html.DisplayFor(modelItem => item.FilePath)</a>
</td>
<td>
@Html.DisplayFor(modelItem => item.Type)
</td>
<td>
@Html.DisplayFor(modelItem => item.UpdateAt)
</td>
<td>
<button asp-page-handler="Delete" asp-route-id="@item.ID" onclick="return confirm('Are you sure you want to delete?')" class="btn btn-danger btn-sm">Delete</button>
</td>
</tr>
}
</tbody>
</table>
</form>
</div> <!-- end card body-->
</div> <!-- end card -->
</div><!-- end col-->
</div>
<!-- end row-->
</div>
@section Scripts {
}