部分视图中的发布操作未触发

时间:2019-07-09 02:20:19

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

嗨,我还在学习MVC。我有一个要求,其中我的页面带有带有表名的dropdownlist。当用户选择表名称时,将调用部分视图以在DataTable中显示数据。

当前,尝试显示DataTable时出现问题。后动作GetDPUserList()未触发,因此数据未显示。

这就是我所说的局部视图:

$('#ddPatchingAction').change(function () {
        ddPatchingActionValue = $('#ddPatchingAction option:selected').val();
        if (ddPatchingActionValue) {
            $.get("/PatchingBatch/DataTableBatch", function (data) {
                $('#divPatching').html(data);
                $('#batchTable').DataTable();
            });
        }
    });

这是局部视图:

<table id="batchTable" class="table-striped table-responsive">
    <thead>
        <tr>
            <th>Username</th>
            <th>Name</th>
            <th>Email Address</th>
            <th>Is Admin</th>
            <th></th>
        </tr>
    </thead>
</table>


<link href="//cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet" />
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">

@section scripts{
    <script src="//cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>

    <script>
        var popup, dataTable;

        $(document).ready(function () {
            dataTable = $("#batchTable").DataTable({
                "ajax": {
                    "url": "/PatchingBatch/GetDPUserList",
                    "type": "POST",
                    "datatype": "json"
                },

                "columns": [
                    { "data": "Username", "name":"Username" },
                    { "data": "Name", "name": "Name"  },
                    { "data": "Email", "name": "Email"  },
                    { "data": "IsAdmin", "name": "IsAdmin" },
                    {
                        "data": "DPUserID", "render": function (data) {
                            return "<a class='btn btn-primary btn-sm' onclick=EditUserForm('@Url.Action("UpdateUser", "Account")/" + data +"')><i class='fa fa-pencil'></i> Edit</a><a class='btn btn-danger btn-sm'  style='margin-left: 5px' onclick=Delete(" + data +")><i class='fa fa-trash'></i> Delete</a>";
                        },
                        "orderable": false,
                        "searchable": false,
                        "width": "150px"
                    },

                ],

                "processing": "true",
                "serverSide": "true",
                "order": [0, "asc"]
            });
        });

        function AddUserForm(url) {
            var formDiv = $('<div/>');

            $.get(url)
                .done(function (response) {
                    formDiv.html(response);

                    popup = formDiv.dialog({
                        autoOpen: true,
                        resizable: false,
                        title: "Add User Account",
                        height: 250,
                        width: 300,
                        close: function () {
                            popup.dialog('destroy').remove();
                        }
                    });
                });
        }

        function EditUserForm(url) {
            var formDiv = $('<div/>');

            $.get(url)
                .done(function (response) {
                    formDiv.html(response);

                    popup = formDiv.dialog({
                        autoOpen: true,
                        resizable: false,
                        title: "Update User Account",
                        height: 410,
                        width: 300,
                        close: function () {
                            popup.dialog('destroy').remove();
                        }
                    });
                });
        }

        function SubmitForm(form) {
            $.validator.unobtrusive.parse(form);

            if ($(form).valid()) {
                $.ajax({
                    type: "POST",
                    url: form.action,
                    data: $(form).serialize(),
                    success: function (data) {
                        if (data.success) {
                            popup.dialog('close');
                            dataTable.ajax.reload();

                            $.notify(data.message, {
                                globalPosition: "top center",
                                className: "success"
                            })
                        }
                    }
                });
            }
            return false;
        }

        function Delete(id) {
            if (confirm("Are you sure you want to delete this data?")) {
                $.ajax({
                    type: "POST",
                    url: '@Url.Action("DeleteUser", "Account")/' + id,
                    success: function (data) {
                        if (data.success) {
                            dataTable.ajax.reload();
                            $.notify(data.message, {
                                globalPosition: "top center",
                                className: "success"
                            })
                        }
                    }
                }
                )
            }
        }
    </script>
}

这是控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using MVCWeb.Models;
using System.Linq.Dynamic;

namespace MVCWeb.Controllers
{
    public class PatchingBatchController : Controller
    {
        // GET: PatchingBatch
        public ActionResult DataTableBatch()
        {
            return PartialView();
        }

        [HttpPost]
        public ActionResult GetDPUserList()
        {
            //server side parameter
            int start = Convert.ToInt32(Request["start"]);
            int length = Convert.ToInt32(Request["length"]);
            string searchValue = Request["search[value]"];
            string sortColumnName = Request["columns[" + Request["order[0][column]"] + "][name]"];
            string sortDirection = Request["order[0][dir]"];

            List<DP_User> userList = new List<DP_User>();
            using (DBModel db = new DBModel())
            {
                userList = db.DP_User.ToList<DP_User>();
                userList = userList.ToList<DP_User>();
                int totalrows = userList.Count();

                //search
                if (!string.IsNullOrEmpty(searchValue))
                {
                    userList = userList.Where(x => (x.Username != null && x.Username.ToString().ToLower().Contains(searchValue)) ||
                    (x.Name != null && x.Name.ToString().ToLower().Contains(searchValue)) ||
                    (x.Email != null && x.Email.ToString().ToLower().Contains(searchValue))).ToList<DP_User>();
                }

                int totalrowsafterfilter = userList.Count();

                //sorting
                if (!string.IsNullOrEmpty(sortColumnName) && !string.IsNullOrEmpty(sortDirection))
                {
                    userList = userList.OrderBy(sortColumnName + " " + sortDirection).ToList<DP_User>();
                }

                //paging
                userList = userList.Skip(start).Take(length).ToList<DP_User>();

                return Json(new { data = userList, draw = Request["draw"], recordsTotal = totalrows, recordsFiltered = totalrowsafterfilter },
                    JsonRequestBehavior.AllowGet);
            }
        }
    }
}

我已在chrome控制台中签入,但没有错误。所以我不确定是什么问题。我的代码有问题吗?请帮忙。谢谢。

0 个答案:

没有答案