为什么数据表在调用时没有被填充?

时间:2019-05-22 07:13:55

标签: c# jquery asp.net model-view-controller datatables

我通过将模型返回到视图来从viewmodel填充数据表。

现在,我在一个下拉列表中选择了一个项目,它调用了一个函数来填充模型,返回到相同的视图,在该视图中应该用新数据重新填充数据集。

cell2

.cs

  @using (Html.BeginForm("Index", "Certificates", FormMethod.Post, new { }))
    {
        {
            VAILCertificates.DAL.Entities.User Users = (VAILCertificates.DAL.Entities.User)Session["User"];

            if (Users.UserGroupID == 1 || Users.UserRoleName == "admin")
            {
                @Html.Label("Clients", htmlAttributes: new { @class = "control-label col-md-1" })
                @Html.DropDownListFor(model => model.SearchInspectionReport.Client_ID, new SelectList(ViewBag.ClientsList, "ID", "Name"),
                   "-Select-",
                 new { @class = "form-control" })
            }
        }

<table id="tblCertificates" style="width:100%" class="table table-bordered  table-hover mt-2">
                    <thead>
                        <tr class="GridHeading">
                            <td>Certification No.</td>
                            <td>File Name</td>
                            <td>Issue Date</td>
                            <td>Details</td>
                            <td>Client</td>
                            <td>Workshop</td>
                            <td>Added By</td>
                            <td>Office</td>
                            <td>Station</td>
                            <td>File</td>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var certificate in this.Model.CertificatesList)
                        {
                            <tr>
                                <td>@certificate.CertificationNo</td>
                                <td>@certificate.FileName</td>
                                <td>@certificate.IssueDate</td>
                                <td>@certificate.Details</td>
                                <td>@certificate.Client</td>
                                <td>@certificate.Workshop</td>
                                <td>@certificate.UserName</td>
                                <td>@certificate.Office</td>
                                <td>@certificate.Station</td>
                                <td class="text-center">
                                    @*<a href="../Downloads/Certificates/@certificate.FileName" target="_blank">

                                        <i class="fa fa-file-text"></i>
                                    </a>*@
                                    <button class="btn btn-info btn-sm" type="submit" id="btnReview" name="btnDownload" formaction='@Url.Action("GetInspectionReportDetails", "InspectionReport", new { InspectionReportID = @certificate.InspectionReportID})'>Download</button>
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>

脚本以再次加载数据表

   CertificatesDAL certificatesDAL;

    [HttpGet]
    public ActionResult Index()
    {
        User userSession = (User)Session["User"];

        if (userSession == null || userSession.UserType == (int) UserTypes.RedirectedFromCTR)
        {
            return RedirectToAction("Login", "Home");
        }

        certificatesDAL = new CertificatesDAL();
        CertificatesViewModel certificatesViewModel = new CertificatesViewModel();

        VAILCertificates.DAL.Entities.User user = new User();
        VAILCertificates.DAL.UserDAL userDAL = new DAL.UserDAL();
        user = userDAL.ValidateUser(userSession.UserName, userSession.Password);

        if (user != null) 
        {
            certificatesViewModel.CertificatesList = certificatesDAL.GetCertificatesListFiltered("", user.ClientName, (user.UserGroupID == 1 || user.UserRoleName == "admin" || user.UserRoleName == "Admin") ? true : false); // 1 = admin
        }      

        ClientsDAL clientsDAL = new ClientsDAL();
        ViewBag.ClientsList = clientsDAL.GetClientsList();

        //List<Certificates> l= certificatesDAL.GetClientsList();

        return View(certificatesViewModel);
    }

要在索引上调用的方法已更改

    <script type="text/javascript">
        $('#tblCertificates').DataTable({
            responsive: true,
            searching: true,
            ordering: false,

        });

        $("#SearchInspectionReport_Client_ID").change(function () {

            var ID = $("#SearchInspectionReport_Client_ID").val(); //Client ID

            data = { 'ID': ID } //Client ID

            table = $("#tblCertificates").DataTable();

            $('#tblCertificates tbody').empty()
            $.get("/CertificatesNew/FillCertificates", data);

        });
    </script>
}

0 个答案:

没有答案