【问题标题】:I need to pass data from table to post handler我需要将数据从表传递到后处理程序
【发布时间】:2019-09-18 13:23:47
【问题描述】:

我需要帮助。我是 razor pages 和 asp.net core 的新手,我需要将 HTML 表中的数据发送到我的 post handler 并保存。

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

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

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

    }

【问题讨论】:

  • 您是否有任何理由使用DisplayFor 并且需要在不更改DenniPlan 中的任何内容的情况下提交?

标签: c# asp.net-core entity-framework-core razor-pages


【解决方案1】:

提交数据需要用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>
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-17
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    相关资源
    最近更新 更多