.Net Razor Pages - 创建和处理对象列表
.Net Razor Pages - Create and process list of objects
需要您对如何实施以下内容的想法。
我需要在 OnPost 上处理 对象列表,然后执行一些业务逻辑,但在这个阶段,我不确定如何使用 RazorPages 逻辑或通常使用 MVC 完成的操作来实现它。
在这个阶段,我不能做的是在 OnPostAsync 上获取 selectedCompany 和 inputFile 上的选定值,我希望这些值来自 formData。
有什么想法吗? TY
查看
(...)
<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>
(...)
ViewModel
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;
}
}
}
解决方案 - https://www.learnrazorpages.com/razor-pages/model-binding#binding-complex-collections
查看
[0].CompanyGuid 和 [0].File 上的“0”显然是 auto-generated序号.
<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>
ViewModel
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; }
}
需要您对如何实施以下内容的想法。
我需要在 OnPost 上处理
在这个阶段,我不能做的是在 OnPostAsync 上获取 selectedCompany 和 inputFile 上的选定值,我希望这些值来自 formData。
有什么想法吗? TY
查看
(...)
<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>
(...)
ViewModel
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;
}
}
}
解决方案 - https://www.learnrazorpages.com/razor-pages/model-binding#binding-complex-collections
查看
[0].CompanyGuid 和 [0].File 上的“0”显然是 auto-generated序号.
<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>
ViewModel
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; }
}