需要您對如何實施以下內容的想法。我需要在 OnPost 上處理 <string,file> 物件串列并在之后執行一些業務邏輯,但是在這個階段,我不確定如何使用 RazorPages 邏輯或通常使用 MVC 來實作它。
在這個階段,我不能做的是在 OnPostAsync 上獲取 selectedCompany 和 inputFile 上的選取值,我希望這些值來自 formData。
有什么想法嗎?泰
看法
(...)
<form method="post" enctype="multipart/form-data">
<div class="border p-3 mt-4">
<table class="table" id="filesToProcess">
<tr>
<td>
<div class="mb-3">
<select>
<option value="" name="selectedCompany">Pick a company ...</option>
@foreach (var company in Model.Companies)
{
<option value="@company.Id">@company.Name</option>
}
</select>
</div>
</td>
<td>
<div class="mb-3">
<div>
<input type="file" name="inputFile" />
</div>
</div>
<td>
</tr>
</table>
</div>
<button type="submit" class="btn btn-primary" style="width:150px">Calculate</button>
</form>
(...)
視圖模型
public class CalculatorModel : PageModel
{
private IHostingEnvironment _environment;
private ICompaniesService _companyService;
private IIndicatorsService _indicatorsService;
//To be presented on the front-end
public List<CompanyDto> Companies { get; set; }
//The initial idea would be that one row on the table of the front-end corresponds to one instance of IndicatorsRequest
[BindProperty]
public List<IndicatorsRequest> IndicatorsRequests { get; set; }
public class IndicatorsRequest
{
public Guid CompanyGuid { get; set; }
public IFormFile File { get; set; }
public List<IndicatorDto> CalculatedIndicators { get; set; }
}
public CalculatorModel(IHostingEnvironment environment, ICompaniesService companyService, IIndicatorsService indicatorsService)
{
_environment = environment;
_companyService = companyService;
_indicatorsService = indicatorsService;
}
public async Task OnGet()
{
Companies = await this._companyService.GetCompanies();
}
public async Task OnPostAsync(IFormCollection formData)
{
try
{
var selectedCompanies = formData.Where(f => f.Key.Contains("selectedCompany")).ToList();
var inputFiles = formData.Where(f => f.Key.Contains("inputFile")).ToList();
//Do some business logic with provided companies and files;
}
catch (Exception ex)
{
throw ex;
}
}
}
uj5u.com熱心網友回復:
解決方案- https://www.learnrazorpages.com/razor-pages/model-binding#binding-complex-collections
看法
[0].CompanyGuid和[0].File上的“0”顯然必須是自動生成的序號。
<td>
<div class="mb-3">
<select name="[0].CompanyGuid"> <<<<<<<<<<<<<<<
<option value="">Pick a company ...</option>
@foreach (var company in Model.Companies)
{
<option value="@company.Id">@company.Name</option>
}
</select>
</div>
</td>
<td>
<div class="mb-3">
<div>
<input type="file" name="[0].File" /> <<<<<<<<<<<<<
</div>
</div>
<td>
視圖模型
public async Task OnPostAsync(List<IndicatorsRequest> requests)
{
Console.WriteLine(requests.ElementAt(0).CompanyGuid);
}
public class IndicatorsRequest
{
public Guid CompanyGuid { get; set; }
public IFormFile File { get; set; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/439743.html
上一篇:一個php頁面中的三個表單操作
