The first OnGet gathers all the data and makes it available. Its is here where I set my property value also, which I thought I could reuse. The problem comes when i try to use OnGetDownload or OnGetView.
OnGet is passed an id parameter, and the later two are passed a filed parameter. The OnGet id parameter is used to setup my property public Employee Employee {get; set;}
The issue I'm having is when trying to reuse the property in either OnGetView or OnGetDownload. *yes my code needs refactoring but I'm new and learning :D
cshtml.cs
public class IndexModel : PageModel
{
private readonly IConfiguration _configuration;
private readonly ICustomer _customer;
private readonly UserManager<IdentityUser> _userManager;
public IndexModel(IConfiguration configuration,
ICustomer customer, UserManager<IdentityUser> userManager)
{
_configuration = configuration;
_customer = customer;
_userManager = userManager;
}
public Employee Employee { get; set; }
public List<AzureFileModel> AzureFileModel { get; private set; } = new List<AzureFileModel>();
public async Task OnGetAsync(int id)
{
Employee = await _customer.GetEmployeeNo(id);
var empNo = Employee.EmployeeNumber; // related table
string fileStorageConnection = _configuration.GetValue<string>("FileStorage");
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(fileStorageConnection);
CloudFileShare share = storageAccount.CreateCloudFileClient().GetShareReference("test");
CloudFileDirectory root = share.GetRootDirectoryReference();
CloudFileDirectory dir = root.GetDirectoryReference(empNo.EmployeeOnline+"/files");
// list all files in the directory
AzureFileModel = await ListSubDir(dir);
}
public static async Task<List<AzureFileModel>> ListSubDir(CloudFileDirectory fileDirectory)
{
var fileData = new List<AzureFileModel>();
FileContinuationToken token = null;
do
{
FileResultSegment resultSegment = await fileDirectory.ListFilesAndDirectoriesSegmentedAsync(token);
foreach (var fileItem in resultSegment.Results)
{
if (fileItem is CloudFile)
{
var cloudFile = (CloudFile) fileItem;
//get the cloudfile's properties and metadata
await cloudFile.FetchAttributesAsync();
// Add properties to FileDataModel
fileData.Add(new AzureFileModel()
{
FileName = cloudFile.Name,
Size = Math.Round((cloudFile.Properties.Length / 1024f), 2).ToString(),
DateModified = DateTime.Parse(cloudFile.Properties.LastModified.ToString()).ToLocalTime()
.ToString()
});
}
if (fileItem is CloudFileDirectory)
{
var cloudFileDirectory = (CloudFileDirectory) fileItem;
await cloudFileDirectory.FetchAttributesAsync();
//list files in the directory
var result = await ListSubDir(cloudFileDirectory);
fileData.AddRange(result);
}
// get the FileContinuationToken to check if we need to stop the loop
token = resultSegment.ContinuationToken;
}
} while (token != null);
return fileData.OrderByDescending(o => Convert.ToDateTime(o.DateModified)).ToList();
}
public async Task<IActionResult> OnGetView(string fileId)
{
var empNo = Employee.EmployeeNumber;
string fileStorageConnection = _configuration.GetValue<string>("FileStorage");
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(fileStorageConnection);
CloudFileShare share = storageAccount.CreateCloudFileClient().GetShareReference("test");
CloudFileDirectory rootDir = share.GetRootDirectoryReference();
CloudFileDirectory dir = rootDir.GetDirectoryReference(empNo.EmployeeOnline+"/files");
CloudFile file = dir.GetFileReference(fileId);
try
{
var stream = await file.OpenReadAsync();
return File(stream, "application/pdf");
}
catch (Exception ex)
{
throw new Exception(String.Format($"An error occurred while executing the view {ex.Message}"));
}
}
public async Task<IActionResult> OnGetDownload(string fileId)
{
removed for brevity
}
}
I have tried passing in a second parameter in OnGetView(string fileId, int id)
but again, I'm not able to retrieve the logged in users id to set it. What am I missing?
cshtml
@page "{id:int}"
@model NavraePortal.WebApp.Pages.Files.IndexModel
@{
ViewData["Title"] = "Documents";
}
<h1>Pay Stub Copies</h1>
<table class="table table-bordered">
<thead>
<tr>
<th>File Name</th>
<th>File Date</th>
<th>Download</th>
<th>View</th>
</tr>
</thead>
<tbody>
@foreach (var data in Model.AzureFileModel)
{
<tr>
<td>@data.FileName</td>
<td>@data.DateModified</td>
<td>
<a class="btn btn-primary" asp-route-fileId="@data.FileName" asp-page-handler="Download">Download</a>
</td>
<td>
<a class="btn btn-info" asp-route-fileId="@data.FileName" asp-page-handler="View">View</a>
</td>
</tr>
}
</tbody>
</table>