Saturday 10 February 2018

asp-action asp-route

delete link in index view
--<a asp-action="DeleteTop5" asp-route-num_to_delete="5">Delete top 5</a>--
action specify controller, route specify controller argument

num_to_delete="5" returns 5 record


num_to_delete="3" returns 3 record

num_to_delete="10" returns 10 record


history controller function

//delete few items from top
public async Task<IActionResult> DeleteTop5(int num_to_delete)
        {
            var items = await _context.History.Take(num_to_delete).ToListAsync();
            return View(items);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteTop5(IFormCollection collection)
        {
            var id_delete = collection["id_num"].ToString().Split(',');

            for (int i = 0; i < id_delete.Length - 1; i++)
            {
                var history = await _context.History.SingleOrDefaultAsync(m => m.Id == Convert.ToInt32(id_delete[i]));
                _context.History.Remove(history);
                await _context.SaveChangesAsync();
            }

            return RedirectToAction(nameof(Index));
        }

-------------------------------------------------------------------------------------------

view index.cshtml

 <a asp-action="DeleteTop5" asp-route-num_to_delete="5">Delete top 5</a>

----------------------------------------------------------------------------------------------

view delete_top_5.cshtml

@model IEnumerable<ebay_track.Models.History>

@{
    ViewData["Title"] = "Delete";
}

<script src="~/lib/jquery/dist/jquery.js"></script>
<script type="text/javascript">

    var num_array='';

    $(document).ready(function () {
        @foreach(var item in Model)
        {
        <text>
        num_array = num_array + '@item.Id' + ',';
        </text>
        }

        $("#id_num").val(num_array);
    });

</script>

<h2>Delete</h2>

<h3>Are you sure you want to delete these?</h3>
<div>
    <h4>History</h4>
    <hr />

    <table class="table">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Id)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.ActionDate)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.ActionType)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Item)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Tracking)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Qty)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.ShippingDate)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.DeliveryDate)
                </th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.Id)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.ActionDate)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.ActionType)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Item)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Tracking)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Qty)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.ShippingDate)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.DeliveryDate)
                    </td>
                </tr>
            }
        </tbody>
    </table>

    <form method="post">
        <input type="hidden" id="id_num" name="id_num"/>
        <input type="submit" value="Delete" class="btn btn-default" /> |
        <a asp-action="Index">Back to List</a>
    </form>
</div>

No comments:

Post a Comment