我需要将数据从表传递到post处理程序

问题描述 投票:0回答:1

我需要帮助。我是razor页面和asp.net核心的新手,我需要将我在HTML表格中的数据发送到我的帖子处理程序并保存它。

我在客户端上重新排序行,然后我需要在按钮单击时保存重新排序的行。

<form method="post">
    <div>

        <table id="example" class="table table-stripped border">
            <thead>
                <tr class="table-secondary">
                    <th>@Html.DisplayNameFor(m => m.DenniPlan.FirstOrDefault().CisloZakazky)</th>
                    <th>@Html.DisplayNameFor(m => m.DenniPlan.FirstOrDefault().CisloProduktu)</th>
                    <th>@Html.DisplayNameFor(m => m.DenniPlan.FirstOrDefault().Poradi)</th>

                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model.DenniPlan)
                {
                    <tr>
                        <td>@Html.DisplayFor(m => item.CisloZakazky)</td>
                        <td>@Html.DisplayFor(m => item.CisloProduktu)</td>
                        <td>@Html.DisplayFor(m => item.Poradi)</td>
                    </tr>
                }
            </tbody>
        </table>
        <button type="submit" class="btn btn-primary" >Časovat</button>
    </div>
</form>

Index.cshtml.cs

    private readonly DataContext _context;
    public IEnumerable<DenniPlan2> DenniPlan { get; set; }      

    public IndexModel(DataContext context)
    {
        _context = context;
    }

    public async Task OnGet()
    {
        DenniPlan = await _context.DenniPlan2.Take(20).OrderBy(x => x.Poradi).ToListAsync();

    }
c# asp.net-core entity-framework-core razor-pages
1个回答
0
投票

要提交数据,您需要使用input呈现数据。否则,您需要使用jquery来循环td节点。但是,IMO,在不改变任何内容的情况下提交数据是不合理的。

  1. 索引页 public class IndexModel : PageModel { private readonly TestRazor.Data.ApplicationDbContext _context; public IndexModel(TestRazor.Data.ApplicationDbContext context) { _context = context; } [BindProperty] public IList<DenniPlan2> DenniPlan2 { get;set; } public async Task OnGetAsync() { DenniPlan2 = await _context.DenniPlan2s.Take(3).ToListAsync(); } public async Task<IActionResult> OnPostAsync() { var r = DenniPlan2; return RedirectToPage("./Index"); } }
  2. 视图 <form method="post"> <table class="table"> <thead> <tr> <th> @Html.DisplayNameFor(model => model.DenniPlan2[0].CisloZakazky) </th> <th> @Html.DisplayNameFor(model => model.DenniPlan2[0].CisloProduktu) </th> <th> @Html.DisplayNameFor(model => model.DenniPlan2[0].Poradi) </th> <th></th> </tr> </thead> <tbody> @for (int i = 0; i < Model.DenniPlan2.Count; i++) { <tr> <td> @Html.EditorFor(model => model.DenniPlan2[i].CisloZakazky) </td> <td> @Html.EditorFor(model => model.DenniPlan2[i].CisloProduktu) </td> <td> @Html.EditorFor(model => model.DenniPlan2[i].Poradi) </td> </tr> } </tbody> </table> <input type="submit" value="submit" /> </form>
© www.soinside.com 2019 - 2024. All rights reserved.