.Net Razor Pages - 创建和处理对象列表

发布于 2025-01-11 17:55:30 字数 3081 浏览 0 评论 0原文

需要您对如何实施以下内容的想法。 我需要在 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;
        }
    }
}

need your thoughts on how the following can be implemented.
I need to process on the OnPost a list of <string,file> objects and do some business logic after, but at this stage, I am unsure of how to implement it either using RazorPages logic or what was usually done with MVC.

At this stage what I can't do is to get on OnPostAsync the picked values on selectedCompany and inputFile, which I was expecting to come from the formData.

Any thoughts? TY

View

(...)
<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;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静 2025-01-18 17:55:30

解决方案 - https://www .learnrazorpages.com/razor-pages/model-binding#binding-complex-collections

查看

上的“0” [0].CompanyGuid[0].File 显然必须是自动生成的序列号。

                    <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; }
    }

Solution - https://www.learnrazorpages.com/razor-pages/model-binding#binding-complex-collections

View

The '0' on [0].CompanyGuid and [0].File has obviously to be an auto-generated sequencial number.

                    <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; }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文