Razor Pages ASP.NET Core - OnPost() 使用 datatables.net 搜索栏不返回任何对象

问题描述

我有一个页面,用于修改数据库提取的对象。加载页面时,LINQ 查询会创建一个对象列表,然后显示在 HTML 表中。用户可以使用下拉菜单选择他们想要进行的更改,然后单击他们想要进行此更改的对象上的复选框。一切正常,直到 datatables.net 搜索栏发挥作用。

使用此搜索栏,可优化列表中可查看的项目数量,使用户更容易进行所需的更改。不幸的是,当用户单击按钮进行这些更改时,什么也没有发生。查看 OnPost(),那里没有返回任何对象,因此无法更新任何内容

这是页面的后端文件

namespace CustomerPageTest.Pages.View
{
    public class EditClassificationTiersModel : PageModel
    {
        [BindProperty(SupportsGet = true)]
        public int returnedClassification { get; set; }

        [BindProperty(SupportsGet = true)]
        public int returnedTier { get; set; }

        [BindProperty(SupportsGet = true)]
        public IEnumerable<vInfoView> vInfoViews { get; set; }

        [BindProperty(SupportsGet = true)]
        public string SearchTerm { get; set; }

        [BindProperty(SupportsGet = true)]
        public int assessmentID { get; set; }

        public List<SelectListItem> ClassificationList { get; set; } = DatabaseClassificationData();

        [BindProperty(SupportsGet = true)]
        public List<SelectListItem> TierList { get; set; }

        public void OnGet(int assessmentId)
        {
            assessmentID = assessmentId;
            TierList = DatabaseTierData();
            vInfoViews = GetvInfoViewsByAssessment(assessmentId);
        }

        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            using (var context = new DataWarehouseContext())
            {
                foreach (var info in vInfoViews)
                {
                    if (info.isChecked)
                    {
                        var temp = context.RvtoolsVInfo
                            .Where(a => a.VmId.Equals(info.vmID))
                            .FirstOrDefault();

                        if(returnedClassification != 0) //If Data Classification form was changed we change it,else it stays the same
                            temp.DataClassificationId = returnedClassification;

                        if(returnedTier != 0) //If Tier form was changed we change it,else it stays the same
                            temp.TierNumber = returnedTier;

                        try
                        {
                            await context.SaveChangesAsync(); //Saves changes on selected objects
                        }
                        catch (Exception)
                        {

                        }
                    }
                }
            }
            return RedirectToPage("/View/EditClassificationTiers",new { assessmentId = assessmentID });
        }

这是页面的前端文件

@page "{assessmentId}"
@model CustomerPageTest.Pages.View.EditClassificationTiersModel
@{
    ViewData["Title"] = "EditClassificationTiers";
}

@section Scripts
{
    <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
    <script src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.min.js"></script>
    <script>
        $(document).ready(function () {
            $('#myTable').dataTable({
                "paging": false,"search": {
                    "smart": false
                }
            });
        });
    </script>

}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.22/css/jquery.dataTables.min.css">

<h1 align="center" style="color:yellowgreen">Edit Classifications / Tiers</h1>
<br />

<form method="post">
    <div class="container">
        <table class="table" style="border: hidden !important; border-bottom: hidden !important; white-space: Nowrap; width: 100%;">
            <tr>
                <td>
                    <h3 class="text-light" style="white-space: Nowrap">Data Classification:</h3>
                    <div class="form-group justify-content-center">
                        <select class="form-control" asp-for="returnedClassification" asp-items="Model.ClassificationList"></select>
                    </div>
                </td>
                <td>
                    <h3 class="text-light" style="white-space: Nowrap">Tier:</h3>
                    <div class="form-group justify-content-center">
                        <select class="form-control" asp-for="returnedTier" asp-items="Model.TierList"></select>
                    </div>
                </td>
                <td>
                    <br /><br /><br />
                    <div class="inner-addon left-addon">
                        <i class="glyphicon glyphicon-plus text-light col-md-offset-6"></i>
                        <input type="submit" value="Add Classification / Tier" class="btn btn-dark text-light col-md-offset-6" />
                    </div>
                </td>
                <td>
                    <br /><br /><br />
                    <div class="inline-right justify-content-center">
                        <a class="btn btn-dark" asp-page="/View/EditAssessment" asp-route-assessmentId="@Model.assessmentID">
                            <i class="glyphicon glyphicon-arrow-left"></i>    Back
                        </a>
                    </div>
                </td>
            </tr>
        </table>
    </div>
    <input type="hidden" asp-for="@Model.assessmentID" name="assessmentID" />
    <table id="myTable" class="display cell-border stripe" role="grid" style="background-color: #dbdbdb; text-align:center; width: 100%">
        <thead>
            <tr class="text-dark">
                <th style="text-align: center; width: 5%;"></th>
                <th style="text-align: center; width: 10%;"><strong>Name</strong></th>
                <th style="text-align: center; width: 10%;"><strong>Datacenter</strong></th>
                <th style="text-align: center; width: 15%;"><strong>Cluster</strong></th>
                <th style="text-align: center; width: 15%;"><strong>Host</strong></th>
                <th style="text-align: center; width: 10%;"><strong>Classification</strong></th>
                <th style="text-align: center; width: 10%;"><strong>Tier</strong></th>
                <th style="text-align: center; width: 25%"><strong>Annotation</strong></th>
            </tr>
        </thead>
        <tbody style="background-color: #dbdbdb;">
            @for (int i = 0; i < Model.vInfoViews.Count(); i++)
            {
                <tr class="text-dark">
                    <td style="padding-top: 15px; width: 5%;">
                        <input type="checkBox" asp-for="@Model.vInfoViews.ElementAt(i).isChecked" name="vInfoViews[@i].isChecked" />
                        <input type="hidden" asp-for="@Model.vInfoViews.ElementAt(i).vHostID" name="vInfoViews[@i].vHostID" />
                        <input type="hidden" asp-for="@Model.vInfoViews.ElementAt(i).assessmentID" name="vInfoViews[@i].assessmentID" />
                        <input type="hidden" asp-for="@Model.vInfoViews.ElementAt(i).vmID" name="vInfoViews[@i].vmID" />
                    </td>
                    <td style="padding-top: 15px; width: 10%;">
                        @Model.vInfoViews.ElementAt(i).Name
                        <input type="hidden" asp-for="@Model.vInfoViews.ElementAt(i).Name" name="vInfoViews[@i].Name" />
                    </td>

                    <td style="padding-top: 15px; width: 10%;">
                        @Model.vInfoViews.ElementAt(i).Datacenter
                        <input type="hidden" asp-for="@Model.vInfoViews.ElementAt(i).Datacenter" name="vInfoViews[@i].Datacenter" />
                    </td>

                    <td style="padding-top: 15px; width: 15%;">
                        @Model.vInfoViews.ElementAt(i).Cluster
                        <input type="hidden" asp-for="@Model.vInfoViews.ElementAt(i).Cluster" name="vInfoViews[@i].Cluster" />
                    </td>

                    <td style="padding-top: 15px; width: 15%;">
                        @Model.vInfoViews.ElementAt(i).hostName
                        <input type="hidden" asp-for="@Model.vInfoViews.ElementAt(i).hostName" name="vInfoViews[@i].hostName" />
                    </td>

                    <td style="padding-top: 15px; width: 10%;">
                        @Model.vInfoViews.ElementAt(i).printClassification
                        <input type="hidden" asp-for="@Model.vInfoViews.ElementAt(i).classificationID" name="vInfoViews[@i].classificationID" />
                    </td>

                    <td style="padding-top: 15px; width: 10%;">
                        @Model.vInfoViews.ElementAt(i).Tier
                        <input type="hidden" asp-for="@Model.vInfoViews.ElementAt(i).Tier" name="vInfoViews[@i].Tier" />
                    </td>

                    <td style="padding-top: 15px; width: 25%;">
                        @Model.vInfoViews.ElementAt(i).Annotation
                        <input type="hidden" asp-for="@Model.vInfoViews.ElementAt(i).Annotation" name="vInfoViews[@i].Annotation" />
                    </td>
                </tr>
            }
        </tbody>
    </table>
</form>

我不能给出一个有效的例子,但我会提供当前问题的屏幕截图。这些第一个屏幕截图是一切正常时发生的情况:

Selecting Row and Entering new information

接下来,这是 OnPost() 的样子:

enter image description here

您可以看到对象已经通过,因此当页面重新加载时,进行了更改:

enter image description here

现在假设我希望使用搜索栏更改带有 Annotation: "PRTG Monitoring" 的行,如下所示:

enter image description here

当我查看 OnPost() 时,这就是我所看到的

enter image description here

列表为空!!

所以,这是一个我不知道如何解决的问题,而且我在其他地方找不到解决方案,你们认为我应该怎么做?有没有办法用与前端每一行中的对象对应的 ID 填充新的 List 对象?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)