将数据从js脚本传输到控制器

时间:2018-09-08 18:03:19

标签: javascript ajax asp.net-core

在视图(Index.cshtml)中,我有一个按钮,当我单击该按钮时,就会触发selectedItems中写入数据的脚本。 selectedItem如何转移到控制器?

按钮:

    <form method="post" asp-action="Delete">
        <button type="submit" onclick="SelectedCheckbox()" class="btn-group col-md-2 col-md-offset-0">Remove</button>
    </form>
JS:

function SelectedCheckbox() {
       var selectedItems = new Array();
       $("input[id='check']:checked").each(function () { selectedItems.push($(this).val()); });
    }

控制器(用户):

        [HttpPost]
        public async Task<ActionResult> Delete(string[] selectedUsers)
        {
...
            return RedirectToAction("Index");
        }
我尝试使用ajax,但某些方法无法解决:

    function SelectedCheckbox()
    {

        $.ajax({
            url: "/Users",
            contentType: "application/json",
            method: "POST",
            data: { parameters: parameters },
            success: function (data) {
            var data = new Array();
                $("input[id='check']:checked").each(function () { data.push($(this).val()); });
                alert(data.result);
            },
            error: function (data) { alert(data.responseText); }

        })
    }

@model IEnumerable<Task1.Models.User>
@{
    ViewBag.Title = "Список пользователей";
}
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
 
 
<div class="btn-toolbar nl container col-md-12" role="toolbar" aria-label="Menu">
 
    <form method="post" asp-action="Delete">
        <button type="submit" onclick="SelectedCheckbox()" name="delete" id="delete" class="btn-group col-md-2 col-md-offset-0">Remove</button>
    </form>
 
    <form method="post" asp-controller="Account" asp-action="LogOff">
        <button type="submit" class="col-md-2 col-md-offset-3 " role="group" aria-label="LogOff" />LogOff</button>
    </form>
</div>
 
<form asp-action="Delete">
    <div class="nl">
        <table class="table table-striped table-bordered ">
            <tbody class="table-responsive ">
                <tr><th><input type="checkbox" class="all" data-id="d1" title="Выбрать все"></th><th>Login</th><th>Date of registration</th><th>Date Last Login</th><th>Block disabled</th></tr>
 
                @foreach (var user in Model)
                {
                    <tr>
                        <td>
                            <input type="checkbox" name="selectedUsers"
                                   id="@user.Id" value="@user.Id"
                                   class="styled">
                        </td>
                        <td>@user.UserName</td>
                        <td>@user.DateOfRegistration</td>
                        <td>@user.DateLastLogin</td>
                        <td>@user.LockoutEnabled</td>
                    </tr>
                }
            </tbody>
 
        </table>
    </div>
</form>

<script type="text/javascript">

    function SelectedCheckbox() {
        var selectedItems = new Array();
        $("input[id='check']:checked").each(function () { selectedItems.push($(this).val()); });
        alert(selectedItems);

        return (selectedItems);
    }
</script>

我需要在删除控制器的方法中从脚本传递所选项目的值

0 个答案:

没有答案