0

I have a dropdown, above a table, when you select the dropdown the selection is not pre-appending the selection. When I click the dropdown then click my table headers to sort them the dropdown selection is lost.

Dropdown

@Html.DropDownList("TenantList", (IEnumerable<SelectListItem>)ViewBag.TenantList, new { id = "ddlSociety", @class = "form-control" })

Table

<th class="sorting text-left hidden-xs hidden-sm @Html.SortTitleItem("SongId", Model.PagingInfo.SortPropertyName, Model.PagingInfo.SortAscending)">
                    <a href="@Url.Action("VerifiedSongs", "SongsManagement", new
                    {
                        songid = songId,
                        page = 1,
                        take = Model.PagingInfo.Take,
                        sortBy = "SongId",
                        sortAsc = Model.PagingInfo.SortPropertyName != "SongId" || !Model.PagingInfo.SortAscending
                    })" data-container="body" data-toggle="tooltip" title="Sort by Song ID">Song ID</a>
                </th>

Controller

 /// <summary>
    /// Verified Songs
    /// </summary>
    /// <param name="uniqueworkid">The accountcode.</param>
    /// <param name="page">The page.</param>
    /// <param name="take">The take.</param>
    /// <param name="sortBy">The sort by.</param>
    /// <param name="sortAsc">if set to <c>true</c> [sort asc].</param>
    /// <returns></returns>
    [HttpGet]
    [Route("VerifiedSongs")]
    [AuthorizeTenancy(Roles = "super,administrator")]
    public ActionResult VerifiedSongs(int page = 1, int take = Constants.MVC.Pagination.DefaultItemsPerPage,
                                            string sortBy = "CreatedDate", bool sortAsc = true)
    {
        // init
        ViewBag.TenantCatalogues = null;
        var model = new ViewModels.VerifiedSongViewModel();
        // Get the paging and sorting parameters from the model (if supplied), else use acceptable defaults.
        var skip = (page * take) - take;
        var songs = GetSongs(skip, take, sortBy, sortAsc);
        var songsTotalCount = GetSongs(0, int.MaxValue, string.Empty, true).ToList().Count;

        //var tenants = GetTenants(skip, take, sortBy, sortAsc);

        model.PagingInfo = new ViewModels.PagingModel
        {
            Page = page,
            Take = take,
            SortAscending = sortAsc,
            SortPropertyName = sortBy,
            Total = songsTotalCount,
            PipedFilter = string.Empty
        };
        model.Songs = songs;
        //model.Tenants = tenants;

        PopulateTenantsDropDownList();

        // AJAX?
        if (!Request.IsAjaxRequest())
        {
            return View(model);
        }

        return PartialView("_VerifiedSongsList", model);
    }

    /// <summary>
    /// Get Verified Songs
    /// </summary>
    /// <param name="uniqueworkid">The accountcode.</param>
    /// <param name="page">The page.</param>
    /// <param name="take">The take.</param>
    /// <param name="sortBy">The sort by.</param>
    /// <param name="sortAsc">if set to <c>true</c> [sort asc].</param>
    /// <returns></returns>
    [HttpGet]
    [Route("GetVerifiedSongs")]
    [AuthorizeTenancy(Roles = "super,administrator")]
    public ActionResult GetVerifiedSongs(string societyId, int page = 1, int take = Constants.MVC.Pagination.DefaultItemsPerPage,
                                            string sortBy = "CreatedDate", bool sortAsc = true)
    {
        //var lookupId = int.Parse(societyId);
        var skip = (page * take) - take;
        //var lookupId = 1;
        var model = GetSongs(skip, take, sortBy, sortAsc);
        return PartialView("_VerifiedSongsList", model);
    }

    /// <summary>
    /// Populate the Tenants/Society Dropdown list
    /// </summary>
    /// <param name="selectedTenant">Selected tenant - if submitted persist state of selection.</param>        
    /// <returns></returns>
    private void PopulateTenantsDropDownList(object selectedTenant = null)
    {
        var tenants = GetTenants(0, int.MaxValue, string.Empty, true);

        var tenantsQuery = from t in tenants
                           orderby t.Name
                           select new
                           {
                               Tenantname = t.Name + " - " + t.TenantGroupName + " - CAEIPI Number: " + t.CAEIPINumber,
                               TenantID = t.ID
                           };
        ViewBag.TenantList = new SelectList(tenantsQuery, "TenantID", "Tenantname", selectedTenant);
    }

    private IEnumerable<VerifiedSongDataViewModel> GetSongs(int skip, int take, string fieldToSort = "SongTitle", bool ascending = true)
    {
        var songs = new List<ViewModels.VerifiedSongDataViewModel>
            {
                new ViewModels.VerifiedSongDataViewModel
                {

                    ID = Guid.NewGuid(),
                    RowVersion = Guid.NewGuid(),
                    IsDeleted = false,

                    SongId = "SEN123456789S",
                    SongTitle = "SongTitle",
                    AccountUniqueCode ="SEN123456789A",
                    CatalogueUniqueCode = "SEN987654321C",
                    CreatedDate = DateTime.UtcNow
            },

JQuery

 @* Sorting Async Partial Handling *@
    $(document).on("click",
        "#tableHeader a",
        function()
        {
            loadPartialPage($(this).attr("href"));
            return false;
        });
    });

Is there a way I can get it to pre-appending the selection, whilst the sorting on the table header is clicked? I am not sure if it is a JQuery issue?

Rob
  • 153
  • 18
  • Why have you marked this as a duplicate as its a completely separate issue to the one you have linked. – Rob Aug 31 '18 at 11:05
  • You have said _the dropdown selection is lost_ - well if you read the dupe you would understand why! And you not even passing the value of the selected option when you redirect, so not sure what your expecting –  Aug 31 '18 at 11:07
  • Because its not posting back from the database as currently it is loading a model of faked data from the controller. So its only using the GET method. – Rob Aug 31 '18 at 11:09
  • I guess you still have not bothered to read the dupe - if you use the same name for the property you bind to (which you have called `TenantList`) and the the `SelectList` (i.e. you have called it `TenantList`) the only the first option will ever be selected when the view is rendered –  Aug 31 '18 at 11:11
  • Okay, so I need to change quite a bit of my code then. I will have a look at the question you have linked to. Thanks – Rob Aug 31 '18 at 11:12
  • And when you click the header you are redirecting. You have not passed the value of the selected option to the GET method, so even if you do correct the naming issue, the controller has no way to know what option to select. –  Aug 31 '18 at 11:13
  • Okay, thanks looks like I will have to do some refactoring – Rob Aug 31 '18 at 11:14
  • But rather than redirecting to reload the whole page, you might consider just updating the table using ajax, in which case there is no issue with the dropdown –  Aug 31 '18 at 11:14
  • The table does load via Ajax. I will add in my code now. – Rob Aug 31 '18 at 11:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179173/discussion-between-stephen-muecke-and-rob). –  Aug 31 '18 at 11:24
  • The way the table works is fine, so I am best rewriting my Dropdown? – Rob Aug 31 '18 at 13:04

0 Answers0