Anas Sadek
Anas Sadek

Reputation: 35

Update the list in the table depending on the selected value of dropdown list

I have a dynamic Dropdown with companies options and I have a table of companies information. I want that my table list change, when I change the company in the Dropdown of companies. Furthermore, I know that I have to use JavaScript, but I don't have any skills with it. please help me.

That is my CSHTML

@page
@model Fachinformationsdienst_Kundenportal.Pages.Information_listModel
@{
}
<div class="form-group col-md-4">
        <label for="inputState">Unternehmen</label>
        <select id="inputState" class="form-control" onchange="ChangeList(this)">
            <option selected>Wählen Sie die Firma aus...</option>
            @for (int i = 0; i < Model.companies.Count; i++)
            {
                <option>@Model.companies[i].FirmenKurzBezeichnung</option>
            }
        </select>
</div>

<table class="table table-striped" id="FachinfoTable">
    <thead>
        <tr>
            <th scope="col">Nr.</th>
            <th scope="col">Name</th>
            <th scope="col">Status</th>
            <th scope="col">Letzte Änderung</th>
            <th scope="col">Aktuelle Version</th>
            <th scope="col">Auftrag</th>
        </tr>
    </thead>
    <tbody>
        @for (int i = 0; i < Model.fachinfos.Count; i++)
        {
            <tr>
                <th scope="row">@Model.fachinfos[i].FachinfoNummer</th>
                <td>@Model.fachinfos[i].FachinfoName</td>
                <td>@Model.fachinfos[i].Status</td>
                <td>@Model.fachinfos[i].Datum</td>
                <td>@Model.fachinfos[i].PdfVersion</td>
                <td>Thornton</td>
            </tr>
        }
    </tbody>
</table>
<script>

Model.cs

using Fachinformationsdienst_Kundenportal.Classes;
using Fachinformationsdienst_Kundenportal.Models;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Collections.Generic;

namespace Fachinformationsdienst_Kundenportal.Pages
{
    public class Information_listModel : PageModel
    {
        public List<Company> companies;
        public List<Fachinfo> fachinfos = new List<Fachinfo>();

        public void OnGet()
        {
            companies = APIRequester.GetCompanies(User.Identity.Name);
            foreach (var company in companies)
            {
                fachinfos.AddRange(APIRequester.GetFachinfos(company.FirmenKurzBezeichnung));
            }
        }
        public List<Fachinfo> GetFachinfosByCompany(string company)
        {
            return APIRequester.GetFachinfos(company);
        }
    }
}

Company Class

namespace Fachinformationsdienst_Kundenportal.Models
{
    public class Company
    {
        public string FirmenKurzBezeichnung { get; set; }
        public string Rolle { get; set; }
    }
}

Information Class

namespace Fachinformationsdienst_Kundenportal.Models
{
    public class Fachinfo
    {
        public int FachinfoNummer { get; set; }
        public string FachinfoName { get; set; }
        public string FirmenKurzBezeichnung { get; set; }
        public string Status { get; set; }
        public string StatusText { get; set; }
        public string Datum { get; set; }
        public string PdfVersion { get; set; }
    }
}

Upvotes: 0

Views: 859

Answers (1)

Zhi Lv
Zhi Lv

Reputation: 21591

You could use a Partial View to display the fachinfos List, then, based on the dropdownlist selected value, use JQuery Ajax to dynamically update the partial view. Please refer the following code:

Create a _FachinfoPartial.cshtml partial view(without page model):

@model List<RazorSample.Models.Fachinfo>

<table class="table table-striped" id="FachinfoTable">
    <thead>
        <tr>
            <th scope="col">Nr.</th>
            <th scope="col">Name</th>
            <th scope="col">Status</th>
            <th scope="col">Letzte Änderung</th>
            <th scope="col">Aktuelle Version</th>
            <th scope="col">Auftrag</th>
        </tr>
    </thead>
    <tbody>
        @for (int i = 0; i < Model.Count; i++)
        {
            <tr>
                <th scope="row">@Model[i].FachinfoNummer</th>
                <td>@Model[i].FachinfoName</td>
                <td>@Model[i].Status</td>
                <td>@Model[i].Datum</td>
                <td>@Model[i].PdfVersion</td>
                <td>Thornton</td>
            </tr>
        }
    </tbody>
</table> 

Update the Information_listModelModel.cshtml page as below (change the namespace ("RazorSample") to yours): using <partial> tag to render the partial view when page load, then, use JQuery Ajax to update the partial view based on the selected company.

@page
@model RazorSample.Pages.Information_listModelModel
@{
}
<div class="form-group col-md-4">
    <label for="inputState">Unternehmen</label>
    <select id="inputState" class="form-control">
        <option selected>Wählen Sie die Firma aus...</option>
        @for (int i = 0; i < Model.companies.Count; i++)
        {
            <option>@Model.companies[i].FirmenKurzBezeichnung</option>
        }
    </select>
</div>
<div id="fachinfoContainer"> 
    <partial name="_FachinfoPartial" model="@Model.fachinfos" />
</div>

@section Scripts{
    <script type="text/javascript">
        $(function () {
            $("#inputState").change(function () {
                var selectcompany = "";
                if ($(this).val() != "Wählen Sie die Firma aus...") {
                    selectcompany = $(this).val();
                } 
                $.ajax({
                    url: "/Information_listModel?handler=fachinfoPartial",
                    type: "Get",
                    data: { company: selectcompany },
                    success: function (result) {
                        $("#fachinfoContainer").html(""); //clear the fachinfo container.
                        $("#fachinfoContainer").html(result); //populate the container.
                    },
                    error: function (result) {
                        alert(result);
                    }
                });
            });
        });
    </script>
}

In the Information_listModel.cshtml.cs page, create a OnGetFachinfoPartial handler to return partial view.

public class Information_listModelModel : PageModel
{
    public List<Company> companies;
    public List<Fachinfo> fachinfos = new List<Fachinfo>();

    public void OnGet()
    {
        //companies = APIRequester.GetCompanies(User.Identity.Name);
        //foreach (var company in companies)
        //{
        //    fachinfos.AddRange(APIRequester.GetFachinfos(company.FirmenKurzBezeichnung));
        //}
        companies = repo.GetCompanies();
        foreach(var company in companies)
        {
            fachinfos.AddRange(repo.GetFachinfos(company.FirmenKurzBezeichnung));
        }

    }

    public PartialViewResult OnGetFachinfoPartial(string company)
    {
        //based on the selctedcompany to filter data, then return to the partial view.
        fachinfos = GetFachinfosByCompany(company);
        return Partial("_FachinfoPartial", fachinfos);
    }
    public List<Fachinfo> GetFachinfosByCompany(string company)
    {
        //return APIRequester.GetFachinfos(company);
        return repo.GetFachinfos(company);
    }
}

The following is the initial data:

public static class repo
{
    public static List<Company> GetCompanies()
    {
        return new List<Company>()
        {
            new Company(){ Rolle="admin", FirmenKurzBezeichnung="Microsoft"},
            new Company(){ Rolle="user", FirmenKurzBezeichnung="Google"},
            new Company(){ Rolle="admin", FirmenKurzBezeichnung="Alibaba"},
            new Company(){ Rolle="user", FirmenKurzBezeichnung="Amazon"},
            new Company(){ Rolle="admin", FirmenKurzBezeichnung="Facebook"},
        };
    }

    public static List<Fachinfo> GetFachinfos(string company)
    {
        var fachinfolist = new List<Fachinfo>()
        {
            new Fachinfo(){ FachinfoNummer=101, FachinfoName="AA", FirmenKurzBezeichnung="Microsoft", Status="Online", Datum= DateTime.Now.ToString()},
            new Fachinfo(){ FachinfoNummer=102, FachinfoName="BB", FirmenKurzBezeichnung="Google", Status="Online", Datum= DateTime.Now.ToString()},
            new Fachinfo(){ FachinfoNummer=103, FachinfoName="CC", FirmenKurzBezeichnung="Alibaba", Status="Online", Datum= DateTime.Now.ToString()},
            new Fachinfo(){ FachinfoNummer=104, FachinfoName="DD", FirmenKurzBezeichnung="Amazon", Status="Online", Datum= DateTime.Now.ToString()},
            new Fachinfo(){ FachinfoNummer=105, FachinfoName="EE", FirmenKurzBezeichnung="Facebook", Status="Online", Datum= DateTime.Now.ToString()},
            new Fachinfo(){ FachinfoNummer=106, FachinfoName="FF", FirmenKurzBezeichnung="Microsoft", Status="Online", Datum= DateTime.Now.ToString()},
            new Fachinfo(){ FachinfoNummer=107, FachinfoName="GG", FirmenKurzBezeichnung="Google", Status="Online", Datum= DateTime.Now.ToString()},
        };

        if (!string.IsNullOrEmpty(company))
        {
            fachinfolist = fachinfolist.Where(c => c.FirmenKurzBezeichnung == company).ToList();
        }

        return fachinfolist;
    }
}

Then, the result like this:

enter image description here

Upvotes: 1

Related Questions