Search code examples
asp.net-corerazorrazor-pages

Razor Page Cascade Datatables by Dropdown


i have a dropdown list and i want to reload the datatable once i change the dropdown please note that the Checkbox field postback the page as well to update the database below is the cs file and will post the cshtml after

public class IndexModel : PageModel
    {
        private readonly IpponAcademy.Models.IJAContext _context;
        public List<SelectListItem> judokaGroupList { get; set; }
       
        [BindProperty]
        public Boolean IsAttend { get; set; }

        

        public IList<tbl_Judoka> tbl_Judoka { get;set; }
        public IndexModel(IpponAcademy.Models.IJAContext context)
        {
            _context = context;
        }
        public void OnGet(Guid? id)
        {
            var GroupList = _context.LK_Groups.ToList();
            judokaGroupList = GroupList.Select(a =>
                                  new SelectListItem
                                  {
                                      Value = a.Group_GUID.ToString(),
                                      Text = a.Group_Name
                                  }).OrderBy(t => t.Text).ToList();

            if (id == null)
            {
                id = Guid.Parse("7F299B82-3397-40F2-8105-65AECB1BA2A8"); //Group A
            }
            tbl_Judoka = _context.tbl_Judokas.Where(c => c.tbl_Judoka_Groups.Any(o => o.Is_Active == true && o.Group_GUID == id)).Include(c => c.tbl_Judoka_Groups.Where(o => o.Is_Active == true && o.Group_GUID == id)).ToList();

        }

        public void OnGetJudoka(Guid? id)
        {
            var GroupList = _context.LK_Groups.ToList();
            judokaGroupList = GroupList.Select(a =>
                                  new SelectListItem
                                  {
                                      Value = a.Group_GUID.ToString(),
                                      Text = a.Group_Name
                                  }).OrderBy(t => t.Text).ToList();

            if (id == null)
            {
                id = Guid.Parse("7F299B82-3397-40F2-8105-65AECB1BA2A8"); //Group A
            }
            
            tbl_Judoka = _context.tbl_Judokas.Where(c => c.tbl_Judoka_Groups.Any(o => o.Is_Active == true && o.Group_GUID == id)).Include(c => c.tbl_Judoka_Groups.Where(o => o.Is_Active == true && o.Group_GUID == id)).ToList();

        }
    }

below is the cshtml file, I'd appreciate finding a simple way to do refresh the datatable with the new selected field from the dropdown

@page
@model IpponAcademy.Pages.Attendance.IndexModel

        
        @{
            ViewData["Title"] = "Index";
            Layout = "~/Pages/Shared/_Layout.cshtml";
        }
        
        
        <form method="post">
       
            <div class="form-group">
                <label class="control-label">Group</label>
                <select id="ddlGroup" class="form-control" asp-items="Model.judokaGroupList"></select>
                @*onchange="alert(@Model.judokaGroupList)"*@
            </div>
        
            <table id="taskDt" class="table table-striped table-bordered nowrap" style="width:100%">
                <thead>
                    <tr>
                        <th>
                            Attended
                        </th>
                        <th>
                            Code
                        </th>
                        <th>
                            Image
                        </th>
                        <th>
                            Judoka
                        </th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var item in Model.tbl_Judoka)
                    {
                        var imagePath = @"UploadedFiles/" + item.J_Image;
                        <tr>
                            <td>
        
                                <input type="hidden" name="J_GUID" id="J_GUID" value="@item.J_GUID" />
        
                                <input asp-for="IsAttend" name="IsAttend" id="IsAttend" type="checkbox" onclick="this.form.submit()" />
        
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.J_Code)
                            </td>
                            <td align="center">
        
                                <img src="@imagePath" alt="Judoka" width="50" height="50" class="rounded-circle mr-1" onclick="return LoadDiv(this.src);" style="cursor: pointer" />
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.J_Name)
                            </td>
        
                        </tr>
                    }
                   
                </tbody>
            </table>

        </form>

        @section scripts{
            <script>
        
                var table;
                function LoadData() {
                    //alert('in');
                    table = $("#taskDt").DataTable({
                        
                        paging: true,
                        responsive: true,
                        searching: true,
                        ordering: true,
                        order: [[1, "asc"]]
                    });
                }
        
                $(document).ready(function () {
                    LoadData();
                    
                })
                $("#ddlGroup").change(function () {
                    alert('DDLGroup');
                    //table.ajax.url("/Attendance/Index?handler=GET&Id=" + $("#ddlGroup Option:Selected").val()).load();
                    window.location.href = '/Attendance/Index?handler=Judoka&Id=' + $("#ddlGroup Option:Selected").val();
                    });

        </script>
        
        }

Solution

  • I think using window.location.href has been a simple enough way to refresh the datatable.Just one thing you need to improve,OnGet and OnGetJudoka method are the same,why not using just one method.

    Change:

    $("#ddlGroup").change(function () {
          window.location.href = '/Attendance/Index?handler=Judoka&Id=' + $("#ddlGroup Option:Selected").val();
    });
    

    To:

    $("#ddlGroup").change(function () {
          window.location.href = '/Attendance/Index?Id=' + $("#ddlGroup Option:Selected").val();
    });
    

    If you must use another way,you could use form.submit():

    Note:Be sure add name="id",otherwise the parameter will not bind to the backend.

    <form method="post" asp-page-handler="Judoka">
    
        <div class="form-group">
            <label class="control-label">Group</label>
            <select id="ddlGroup" class="form-control" name="id" asp-items="Model.judokaGroupList" 
                                                       onchange="this.form.submit()"></select>         
        </div>
    </form>
    

    Backend code:

    Change:

    public void OnGetJudoka(Guid? id)
    

    To:

    public void OnPostJudoka(Guid? id)
    

    BTW,I also have a suggestion that you'd better maintain the selected value after postback :

    public void OnGetJudoka(Guid? id)
    {
        var GroupList = _context.LK_Groups.ToList();
        judokaGroupList = GroupList.Select(a =>
                                new SelectListItem
                                {
                                    Value = a.Group_GUID.ToString(),
                                    Text = a.Group_Name
                                }).OrderBy(t => t.Text).ToList();
        //maintain the selected value after post back...
        var selectedValue= judokaGroupList.Where(a => a.Value == id.ToString()).FirstOrDefault();
        selectedValue.Selected = true;
        
        //...
    
    }