Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have view is C#:

@{
    var itemList = (List<Item>)ViewData["itemsList"];
}
<div class="row" style="margin-top: 10px;">
    <div class="col-md-6">
        @if (itemList != null)
        {
            var id = 0;
            <table class="table table-striped">
                <thead>
                <tr>
                    <th>#</th>
                    <th></th>
                    <th>Id</th>
                    <th>Type</th>
                </tr>
                </thead>
                <tbody>
                    @foreach (var result in itemsList)
                    {
                        <tr>
                            <td>@(++id)</td>
                            <td><input type="checkbox" value="true" @(result.Checked ? "checked" : "")></td>
                            <td>@result.Id</td>
                            <td>@result.Type</td>
                        </tr>
                    }
                </tbody>
            </table>
        }
        <div class="row justify-content-end" style="margin-top: 20px;">
            <div class="col-md-2">
                <form asp-controller="Control" asp-action="Remove" method="post">
                    <input type="hidden" name="tableName" value="table"/>
                    <input type="hidden" name="items" value="@itemList"/>
                    <div style="margin-left: -10px;" class="col-md-2">
                        <button class="btn btn-danger" title="Remove" type="submit">Remove</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>


What I have tried:

I want to remove items from table, where user checks the checkbox. My idea was to update each checked item withing the list (result.Checked property) and then send array to Remove method:

[HttpPost]
        public async Task<IActionResult> Remove(string tableName, List<ChangeQueueItem> items)
        {
            try
            {
                var toDelete = items.Where(x => x.Checked == true);

                await _repository.RemoveFromQueue(toDelete, tableName);
            }
            catch (Exception e)
            {
                TempData["error"] = e.Message;
            }
            return RedirectToAction("Index");
        }


I am trying to send that list like this:
<input type="hidden" name="items" value="@itemList"/> 


initial data is loaded here:
[HttpGet]
        public async Task<IActionResult> Index()
        {
             var items = await _repository.GetAll();
             
            ViewData["itemsList"] = items;
            ViewData["error"] = TempData["error"];

            return View("Index");
        }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900