.NET Core 2.2 Razor Page View当从收集中删除对象时无法正确更新



ASP .NET Core的新手,但是尝试创建Dapper Crud网格,我无法弄清楚为什么当我使用删除后处理程序从列表中删除某个行时,它总是会始终返回列表,最后一行删除而不是选定的列表。

调试OnPostDelete显示其从列表中删除了正确的行。
测试的重新维度按预期工作((,但不在onpostdelete中。

以下是我的大部分代码

背后的代码
 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using Microsoft.Extensions.Configuration;
    using RazorPagesExample.Entity;
    using System.Data.SqlClient;
    using Dapper;
    namespace RazorPagesExample.Pages
    {
        public class multirowformModel : PageModel 
        {
            public class Proddaplocal
            {
                public int? Id { get; set; }
                public string Name { get; set; }
                public string Model { get; set; }
                public int Price { get; set; }
               public string Country { get; set; }
            }        
            [BindProperty]        
            public List<Proddaplocal> products { get; set; }        
            IConfiguration config;
            string connectionString;
            public multirowformModel(IConfiguration configuration)
            {
                if (configuration != null)
                {
                    config = configuration;
                    connectionString = config.GetSection("ConnectionStrings").GetSection("ProductContext").Value;
                }            
            }
            public void OnGet()
            {            
                using (var con = new SqlConnection(connectionString))
                {
                    try
                    {
                        con.Open();
                        var query = "SELECT Id,Name,Model,Price,Country FROM Product";
                        products = con.Query<Proddaplocal>(query).ToList();                 
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        con.Close();
                    }
                }
            }
            public IActionResult OnPostDelete(int? id)
            {
                if (ModelState.IsValid)
                {
                    if (id == null)
                    {
                        return NotFound();
                    }
                    int delid = id ?? 0;
                    products.RemoveAt(delid);
                }
                return Page();
           }
       }
   }

下面是我的剃须刀视图

  @page
    @model RazorPagesExample.Pages.multirowformModel
    @{
    ViewData["Title"] = "form table";
    }
    <h1>datatables</h1>
    <form id=multiform method="post" >
    <div class="table-responsive" style="max-width:800px">
        <table class="table table-sm">
            <thead class="thead-dark">
                <tr>
                    <th scope="col" style="width:30%;min-width:100px">
                        @Html.DisplayNameFor(model => model.products[0].Name)
                    </th>
                    <th scope="col" style="min-width:100px">
                        @Html.DisplayNameFor(model => model.products[0].Model)
                    </th>
                    <th scope="col" style="width:15%; min-width:100px">
                        @Html.DisplayNameFor(model => model.products[0].Price)
                    </th>
                    <th scope="col" style="min-width:100px">
                        @Html.DisplayNameFor(model => model.products[0].Country)
                    </th>
                    <th scope="col" style="min-width:50px">Actions</th>
                </tr>
            </thead>
            <tbody>
                @for (int i = 0; i < Model.products.Count; i++)
                {
                    <tr>
                        <td>
                            <input asp-for="@Model.products[i].Id" type="hidden" />
                            <input asp-for="@Model.products[i].Name" class="form-control" />
                        </td>
                        <td>
                            <input asp-for="@Model.products[i].Model" class="form-control" />
                        </td>
                        <td>
                            <input asp-for="@Model.products[i].Price" class="form-control" />
                        </td>
                        <td>
                            <input asp-for="@Model.products[i].Country" class="form-control" />
                        </td>
                        <td class="table-action">                            
                            <img src="~/icons/trash.svg" title="Delete" alt="Delete" height="18" width="18" onclick="DeleteRow(@i,'@Model.products[i].Name')">
                        </td>
                    </tr>
                }
            </tbody>
        </table>
        <button type="submit" class="btn btn-secondary btn-sm" asp-page-handler="Insert" value="Insert A">
            <i class="fas fa-save">Insert</i>
        </button>
        <button type="submit" class="btn btn-secondary btn-sm" asp-page-handler="Update" value="Update A">
            <i class="fas fa-save">Save</i>
        </button>
    </div>
    </form>
    <SCRIPT language="JavaScript" type="text/Javascript">
    <!--
    function DeleteRow(idno,desc) {
        if (confirm("Are you sure you wish to Delete Row " + desc)) {
            $('#multiform').attr('action', "multirowform?handler=Delete&id=" + idno);
             $("#multiform").submit();
          } else {            
          }
    }
    </SCRIPT>

添加modelState.clear((删除已解决我的问题之前。

        public IActionResult OnPostDelete(int? id)
        {
            if (ModelState.IsValid)
            {
                if (id == null)
                {
                    return NotFound();
                }
                int delid = id ?? 0;
                ModelState.Clear();
                products.RemoveAt(delid);
            }
            return Page();
       }

"如果您要从表单中获取模型,并且要操纵来自客户端表单的数据并将其写回视图,则需要调用ModelState.Clear((以清洁模型State值。"https://patrickdesjardins.com/blog/modelstate-clear-is-required-to-display-back-your-model-object

最新更新