PartialView未更新

时间:2017-10-03 07:43:40

标签: asp.net ajax asp.net-core asp.net-core-mvc partial-views

尝试使用部分视图更新表数据而不重新加载。为什么在控制器操作后我的表没有更新?

在我的部分视图中 _usersTable

@model IEnumerable<Task1.Models.ApplicationUser>

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Email)
            @Html.CheckBoxFor(modelItem => item.userCheck, new { id = item.Id }) 
        </td>
    </tr>
}

在我的主视图中:

<table id="myTable">
    @Html.Partial("_usersTable")
</table>

<script>
    function deleteRecords() {
        var selected = [];
        $('#myTable input:checked').each(function () {
            selected.push($(this).attr('id'));
        });

        $.ajax({
            url: '/Home/DeleteRecords',
            type: 'POST',
            traditional: true,
            data: { array: selected }
        });
    }
</script>

控制器动作:

    [HttpPost]
    public IActionResult DeleteRecords(string[] array)
    {
        using (var context = new ApplicationDbContext(getContextOptions()))
        {
            for (int i = 0; i < array.Count(); i++)
            {
                ApplicationUser user = context.Users.Find(array[i]);
                context.Users.Remove(user);
            }
            context.SaveChanges();          
            return PartialView("_usersTable", context.Users.ToList());
        }
    }

0 个答案:

没有答案