【发布时间】:2021-02-18 16:04:54
【问题描述】:
第一个 OnGet 收集所有数据并使其可用。在这里我也设置了我的属性值,我认为我可以重用它。当我尝试使用 OnGetDownload 或 OnGetView 时出现问题。
OnGet 传递一个id 参数,后面两个传递一个filed 参数。 OnGet id 参数用于设置我的属性public Employee Employee {get; set;}
我遇到的问题是尝试在 OnGetView 或 OnGetDownload 中重用该属性时。 *是的,我的代码需要重构,但我是新手并且正在学习: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
}
}
我已经尝试在OnGetView(string fileId, int id) 中传递第二个参数,但同样,我无法检索已登录的用户 ID 来设置它。我错过了什么?
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>
【问题讨论】:
-
当您单击“查看”或“下载”链接时,您是否希望您的 Employee 变量仍然有效?您是否尝试调试代码?
-
我确实希望它能够保存从 OnGet 到 OnGetView 的值,但事实并非如此。当我在调试中运行时,它会抛出空异常。
-
这正是它应该如何工作的。请记住,您使用的不是可以在用户交互之间维护状态的桌面应用程序。如果您在构造函数中放置一个断点,您将看到每次单击链接时都会一次又一次地调用它。
-
我不知道我做了什么,但它现在可以工作了:/ 我将
int id添加到 OnGetView 参数并填充了它。我以前这样做过,什么也没做。太有趣了! -
我的回答有用吗?
标签: c# azure asp.net-core razor entity-framework-core