Change Data in DataTable with Select from Dropdownlist












0















I have a view with a Datatable, and I want to change the data every time I select a category from a drop-down list.



I want to only display the albums from the selected category, or all albums from all categories, using Ajax and jQuery. The drop-down list must be placed above the table.



Here is my code:



@using System.Collections.Generic;
@using CakeStore.App.Areas.Admin.Models.Albums;
@using CakeStore.App.Services.Contracts;
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

@inject IAlbumService AlbumService


@{ ViewData["Title"] = "Category Albums";
Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
var albums = ViewBag.CategoryAlbums as List<AlbumTableViewModel>;

}

<h1 class="text-center text-header-page">Category Albums</h1>
<hr class="hr-admin-divider" />
<div class="form-group">
<a class="btn button-black-white d-flex justify-content-left" href="/Admin/Albums/Create">Create</a>
</div>
<hr class="hr-admin-divider" />

<div class="d-flex">

<table class="table table-striped table-hover" id="myTable">
<tr>
<th>#</th>
<th>Name</th>
<th>Category</th>
<th></th>
<th></th>
<th></th>
</tr>
@for (int i = 0; i < albums.Count(); i++) {

<tr>
<td class="col-md-1">@albums[i].Id</td>
<td class="col-md-3">@albums[i].Name</td>
<td class="col-md-2">@albums[i].Category</td>
<td><a class="btn button-table-edit" href="/Admin/Albums/Edit?id=@albums[i].Id">EDIT</a></td>
<td><a class="btn button-table-delete d-flex justify-content-right" href="/Admin/Albums/Delete?id=@albums[i].Id">DELETE</a></td>
<td><a class="btn button-table-view d-flex justify-content-right" href="/Admin/Products/CategoryAlbums?id=@albums[i].Id">PRODUCTS</a></td>
</tr>
}
</table>
</div>
<div class="row d-flex align-items-end flex-column" style="font-size:12px">
<a class="btn-link pull-right col-lg-2" asp-controller="Categories" asp-action="Category">Back to Categories</a>
</div>


@section Scripts {
<partial name="_ValidationScriptsPartial" />
}









share|improve this question

























  • Welcome to StackOVerflow. I included the code in the github reference inside your question, as you may later change the content on github, which would (negatively) affect this question. Please edit your question to actually ask a question. Note that there is not often much enthusiasm to write the code for the asker. You should at least show your attempt and tell us where you bump into an issue.

    – trincot
    Jan 1 at 19:29


















0















I have a view with a Datatable, and I want to change the data every time I select a category from a drop-down list.



I want to only display the albums from the selected category, or all albums from all categories, using Ajax and jQuery. The drop-down list must be placed above the table.



Here is my code:



@using System.Collections.Generic;
@using CakeStore.App.Areas.Admin.Models.Albums;
@using CakeStore.App.Services.Contracts;
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

@inject IAlbumService AlbumService


@{ ViewData["Title"] = "Category Albums";
Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
var albums = ViewBag.CategoryAlbums as List<AlbumTableViewModel>;

}

<h1 class="text-center text-header-page">Category Albums</h1>
<hr class="hr-admin-divider" />
<div class="form-group">
<a class="btn button-black-white d-flex justify-content-left" href="/Admin/Albums/Create">Create</a>
</div>
<hr class="hr-admin-divider" />

<div class="d-flex">

<table class="table table-striped table-hover" id="myTable">
<tr>
<th>#</th>
<th>Name</th>
<th>Category</th>
<th></th>
<th></th>
<th></th>
</tr>
@for (int i = 0; i < albums.Count(); i++) {

<tr>
<td class="col-md-1">@albums[i].Id</td>
<td class="col-md-3">@albums[i].Name</td>
<td class="col-md-2">@albums[i].Category</td>
<td><a class="btn button-table-edit" href="/Admin/Albums/Edit?id=@albums[i].Id">EDIT</a></td>
<td><a class="btn button-table-delete d-flex justify-content-right" href="/Admin/Albums/Delete?id=@albums[i].Id">DELETE</a></td>
<td><a class="btn button-table-view d-flex justify-content-right" href="/Admin/Products/CategoryAlbums?id=@albums[i].Id">PRODUCTS</a></td>
</tr>
}
</table>
</div>
<div class="row d-flex align-items-end flex-column" style="font-size:12px">
<a class="btn-link pull-right col-lg-2" asp-controller="Categories" asp-action="Category">Back to Categories</a>
</div>


@section Scripts {
<partial name="_ValidationScriptsPartial" />
}









share|improve this question

























  • Welcome to StackOVerflow. I included the code in the github reference inside your question, as you may later change the content on github, which would (negatively) affect this question. Please edit your question to actually ask a question. Note that there is not often much enthusiasm to write the code for the asker. You should at least show your attempt and tell us where you bump into an issue.

    – trincot
    Jan 1 at 19:29
















0












0








0


1






I have a view with a Datatable, and I want to change the data every time I select a category from a drop-down list.



I want to only display the albums from the selected category, or all albums from all categories, using Ajax and jQuery. The drop-down list must be placed above the table.



Here is my code:



@using System.Collections.Generic;
@using CakeStore.App.Areas.Admin.Models.Albums;
@using CakeStore.App.Services.Contracts;
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

@inject IAlbumService AlbumService


@{ ViewData["Title"] = "Category Albums";
Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
var albums = ViewBag.CategoryAlbums as List<AlbumTableViewModel>;

}

<h1 class="text-center text-header-page">Category Albums</h1>
<hr class="hr-admin-divider" />
<div class="form-group">
<a class="btn button-black-white d-flex justify-content-left" href="/Admin/Albums/Create">Create</a>
</div>
<hr class="hr-admin-divider" />

<div class="d-flex">

<table class="table table-striped table-hover" id="myTable">
<tr>
<th>#</th>
<th>Name</th>
<th>Category</th>
<th></th>
<th></th>
<th></th>
</tr>
@for (int i = 0; i < albums.Count(); i++) {

<tr>
<td class="col-md-1">@albums[i].Id</td>
<td class="col-md-3">@albums[i].Name</td>
<td class="col-md-2">@albums[i].Category</td>
<td><a class="btn button-table-edit" href="/Admin/Albums/Edit?id=@albums[i].Id">EDIT</a></td>
<td><a class="btn button-table-delete d-flex justify-content-right" href="/Admin/Albums/Delete?id=@albums[i].Id">DELETE</a></td>
<td><a class="btn button-table-view d-flex justify-content-right" href="/Admin/Products/CategoryAlbums?id=@albums[i].Id">PRODUCTS</a></td>
</tr>
}
</table>
</div>
<div class="row d-flex align-items-end flex-column" style="font-size:12px">
<a class="btn-link pull-right col-lg-2" asp-controller="Categories" asp-action="Category">Back to Categories</a>
</div>


@section Scripts {
<partial name="_ValidationScriptsPartial" />
}









share|improve this question
















I have a view with a Datatable, and I want to change the data every time I select a category from a drop-down list.



I want to only display the albums from the selected category, or all albums from all categories, using Ajax and jQuery. The drop-down list must be placed above the table.



Here is my code:



@using System.Collections.Generic;
@using CakeStore.App.Areas.Admin.Models.Albums;
@using CakeStore.App.Services.Contracts;
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

@inject IAlbumService AlbumService


@{ ViewData["Title"] = "Category Albums";
Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
var albums = ViewBag.CategoryAlbums as List<AlbumTableViewModel>;

}

<h1 class="text-center text-header-page">Category Albums</h1>
<hr class="hr-admin-divider" />
<div class="form-group">
<a class="btn button-black-white d-flex justify-content-left" href="/Admin/Albums/Create">Create</a>
</div>
<hr class="hr-admin-divider" />

<div class="d-flex">

<table class="table table-striped table-hover" id="myTable">
<tr>
<th>#</th>
<th>Name</th>
<th>Category</th>
<th></th>
<th></th>
<th></th>
</tr>
@for (int i = 0; i < albums.Count(); i++) {

<tr>
<td class="col-md-1">@albums[i].Id</td>
<td class="col-md-3">@albums[i].Name</td>
<td class="col-md-2">@albums[i].Category</td>
<td><a class="btn button-table-edit" href="/Admin/Albums/Edit?id=@albums[i].Id">EDIT</a></td>
<td><a class="btn button-table-delete d-flex justify-content-right" href="/Admin/Albums/Delete?id=@albums[i].Id">DELETE</a></td>
<td><a class="btn button-table-view d-flex justify-content-right" href="/Admin/Products/CategoryAlbums?id=@albums[i].Id">PRODUCTS</a></td>
</tr>
}
</table>
</div>
<div class="row d-flex align-items-end flex-column" style="font-size:12px">
<a class="btn-link pull-right col-lg-2" asp-controller="Categories" asp-action="Category">Back to Categories</a>
</div>


@section Scripts {
<partial name="_ValidationScriptsPartial" />
}






asp.net-core-mvc asp.net-ajax






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 19:29









trincot

127k1688123




127k1688123










asked Jan 1 at 13:44









Vladimir PetukhovVladimir Petukhov

124




124













  • Welcome to StackOVerflow. I included the code in the github reference inside your question, as you may later change the content on github, which would (negatively) affect this question. Please edit your question to actually ask a question. Note that there is not often much enthusiasm to write the code for the asker. You should at least show your attempt and tell us where you bump into an issue.

    – trincot
    Jan 1 at 19:29





















  • Welcome to StackOVerflow. I included the code in the github reference inside your question, as you may later change the content on github, which would (negatively) affect this question. Please edit your question to actually ask a question. Note that there is not often much enthusiasm to write the code for the asker. You should at least show your attempt and tell us where you bump into an issue.

    – trincot
    Jan 1 at 19:29



















Welcome to StackOVerflow. I included the code in the github reference inside your question, as you may later change the content on github, which would (negatively) affect this question. Please edit your question to actually ask a question. Note that there is not often much enthusiasm to write the code for the asker. You should at least show your attempt and tell us where you bump into an issue.

– trincot
Jan 1 at 19:29







Welcome to StackOVerflow. I included the code in the github reference inside your question, as you may later change the content on github, which would (negatively) affect this question. Please edit your question to actually ask a question. Note that there is not often much enthusiasm to write the code for the asker. You should at least show your attempt and tell us where you bump into an issue.

– trincot
Jan 1 at 19:29














1 Answer
1






active

oldest

votes


















0














You could use the Partial View ,I made the demo ,you could refer to it



Use ajax to call the GetCityList method to get the data corresponding to the countryId passed .



<div class="form-group">
<label class="col-form-label">Country</label>
<select class="form-control" asp-items="@ViewBag.Country" id="selectlist">
<option>All</option>
</select>
</div>

<div class="form-group" id="cityListWrapper">
<partial name="_CityListPartial" model="@ViewBag.City" />
</div>

@section Scripts
{
<script type="text/javascript">
$("#selectlist").change(function () {
var countryId = $("#selectlist").val();
$.ajax
({
type: "GET",
url: "/Countries/GetCityList?CountryId="+countryId,
success: function (result) {
$("#cityListWrapper").html(result)
}
});

});
</script>
}


Initial rendering the main view , show all albums when not selected



 public async Task<IActionResult> Index()
{
ViewBag.Country = new SelectList(_context.Country, "Id", "CountryName");
ViewBag.City = _context.City.ToList();

return View();
}


The GetCityList action ,render the partial view using a statement that returns different values



 [HttpGet]
public async Task<IActionResult> GetCityList(int? countryId)
{
if (countryId == null)
{
var CityList = await _context.City.ToListAsync();
return PartialView("_CityListPartial", CityList);
}
var Cities =await _context.City.Where(c => c.Country.Id == countryId).ToListAsync();
return PartialView("_CityListPartial", Cities);
}


How it works



enter image description here






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995951%2fchange-data-in-datatable-with-select-from-dropdownlist%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You could use the Partial View ,I made the demo ,you could refer to it



    Use ajax to call the GetCityList method to get the data corresponding to the countryId passed .



    <div class="form-group">
    <label class="col-form-label">Country</label>
    <select class="form-control" asp-items="@ViewBag.Country" id="selectlist">
    <option>All</option>
    </select>
    </div>

    <div class="form-group" id="cityListWrapper">
    <partial name="_CityListPartial" model="@ViewBag.City" />
    </div>

    @section Scripts
    {
    <script type="text/javascript">
    $("#selectlist").change(function () {
    var countryId = $("#selectlist").val();
    $.ajax
    ({
    type: "GET",
    url: "/Countries/GetCityList?CountryId="+countryId,
    success: function (result) {
    $("#cityListWrapper").html(result)
    }
    });

    });
    </script>
    }


    Initial rendering the main view , show all albums when not selected



     public async Task<IActionResult> Index()
    {
    ViewBag.Country = new SelectList(_context.Country, "Id", "CountryName");
    ViewBag.City = _context.City.ToList();

    return View();
    }


    The GetCityList action ,render the partial view using a statement that returns different values



     [HttpGet]
    public async Task<IActionResult> GetCityList(int? countryId)
    {
    if (countryId == null)
    {
    var CityList = await _context.City.ToListAsync();
    return PartialView("_CityListPartial", CityList);
    }
    var Cities =await _context.City.Where(c => c.Country.Id == countryId).ToListAsync();
    return PartialView("_CityListPartial", Cities);
    }


    How it works



    enter image description here






    share|improve this answer




























      0














      You could use the Partial View ,I made the demo ,you could refer to it



      Use ajax to call the GetCityList method to get the data corresponding to the countryId passed .



      <div class="form-group">
      <label class="col-form-label">Country</label>
      <select class="form-control" asp-items="@ViewBag.Country" id="selectlist">
      <option>All</option>
      </select>
      </div>

      <div class="form-group" id="cityListWrapper">
      <partial name="_CityListPartial" model="@ViewBag.City" />
      </div>

      @section Scripts
      {
      <script type="text/javascript">
      $("#selectlist").change(function () {
      var countryId = $("#selectlist").val();
      $.ajax
      ({
      type: "GET",
      url: "/Countries/GetCityList?CountryId="+countryId,
      success: function (result) {
      $("#cityListWrapper").html(result)
      }
      });

      });
      </script>
      }


      Initial rendering the main view , show all albums when not selected



       public async Task<IActionResult> Index()
      {
      ViewBag.Country = new SelectList(_context.Country, "Id", "CountryName");
      ViewBag.City = _context.City.ToList();

      return View();
      }


      The GetCityList action ,render the partial view using a statement that returns different values



       [HttpGet]
      public async Task<IActionResult> GetCityList(int? countryId)
      {
      if (countryId == null)
      {
      var CityList = await _context.City.ToListAsync();
      return PartialView("_CityListPartial", CityList);
      }
      var Cities =await _context.City.Where(c => c.Country.Id == countryId).ToListAsync();
      return PartialView("_CityListPartial", Cities);
      }


      How it works



      enter image description here






      share|improve this answer


























        0












        0








        0







        You could use the Partial View ,I made the demo ,you could refer to it



        Use ajax to call the GetCityList method to get the data corresponding to the countryId passed .



        <div class="form-group">
        <label class="col-form-label">Country</label>
        <select class="form-control" asp-items="@ViewBag.Country" id="selectlist">
        <option>All</option>
        </select>
        </div>

        <div class="form-group" id="cityListWrapper">
        <partial name="_CityListPartial" model="@ViewBag.City" />
        </div>

        @section Scripts
        {
        <script type="text/javascript">
        $("#selectlist").change(function () {
        var countryId = $("#selectlist").val();
        $.ajax
        ({
        type: "GET",
        url: "/Countries/GetCityList?CountryId="+countryId,
        success: function (result) {
        $("#cityListWrapper").html(result)
        }
        });

        });
        </script>
        }


        Initial rendering the main view , show all albums when not selected



         public async Task<IActionResult> Index()
        {
        ViewBag.Country = new SelectList(_context.Country, "Id", "CountryName");
        ViewBag.City = _context.City.ToList();

        return View();
        }


        The GetCityList action ,render the partial view using a statement that returns different values



         [HttpGet]
        public async Task<IActionResult> GetCityList(int? countryId)
        {
        if (countryId == null)
        {
        var CityList = await _context.City.ToListAsync();
        return PartialView("_CityListPartial", CityList);
        }
        var Cities =await _context.City.Where(c => c.Country.Id == countryId).ToListAsync();
        return PartialView("_CityListPartial", Cities);
        }


        How it works



        enter image description here






        share|improve this answer













        You could use the Partial View ,I made the demo ,you could refer to it



        Use ajax to call the GetCityList method to get the data corresponding to the countryId passed .



        <div class="form-group">
        <label class="col-form-label">Country</label>
        <select class="form-control" asp-items="@ViewBag.Country" id="selectlist">
        <option>All</option>
        </select>
        </div>

        <div class="form-group" id="cityListWrapper">
        <partial name="_CityListPartial" model="@ViewBag.City" />
        </div>

        @section Scripts
        {
        <script type="text/javascript">
        $("#selectlist").change(function () {
        var countryId = $("#selectlist").val();
        $.ajax
        ({
        type: "GET",
        url: "/Countries/GetCityList?CountryId="+countryId,
        success: function (result) {
        $("#cityListWrapper").html(result)
        }
        });

        });
        </script>
        }


        Initial rendering the main view , show all albums when not selected



         public async Task<IActionResult> Index()
        {
        ViewBag.Country = new SelectList(_context.Country, "Id", "CountryName");
        ViewBag.City = _context.City.ToList();

        return View();
        }


        The GetCityList action ,render the partial view using a statement that returns different values



         [HttpGet]
        public async Task<IActionResult> GetCityList(int? countryId)
        {
        if (countryId == null)
        {
        var CityList = await _context.City.ToListAsync();
        return PartialView("_CityListPartial", CityList);
        }
        var Cities =await _context.City.Where(c => c.Country.Id == countryId).ToListAsync();
        return PartialView("_CityListPartial", Cities);
        }


        How it works



        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 11:22









        Xueli ChenXueli Chen

        42115




        42115
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995951%2fchange-data-in-datatable-with-select-from-dropdownlist%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            SQL update select statement

            'app-layout' is not a known element: how to share Component with different Modules