Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.
I'm in the process of developing a blazor program. The page contains a drop down list, a button and a table. 
I need to select a client from the drop down list to retrieve the data for the client from the Database. 
But when I press the button, all clients come from the database in the table. I enclose the codes below.
Is there anyone can help me with that?


What I have tried:

@page "/Kontoplan"


@using BOGF.Models
@using Gateway


<h1>Kontoplan</h1>

<EditForm Model="@klient">
    <div>
        <InputSelect @bind-Value="KlientId" name="KlientId" id="KlientId" class="form-control" style="width: auto; float: right">
            <option selected disabled ="">-- Vælg en Klient --</option>
            @foreach (var klient in klienter)
            {
                <option id="@klient.Id" name="@klient.Name">@klient.Id- @klient.Name</option>
            }
        </InputSelect>
        <button type="submit" class="btn btn-primary" style="width:100px; float:right" @onclick="@OnInitializedKontoplan">Hent</button>
    </div>
    @if (kontoplans is null)
    {
        <div class="spinner"></div>
    }
    else
    {
        <table class="table table-striped">
            <thead>
                <tr>
                    <th style="width:110px">LåsKonto</th>
                    <th>Kontonummer</th>
                    <th>Navn</th>
                    <th>Type</th>
                    <th>Moms</th>
                    <th>LockKonto</th>
                    <th>KlientId</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var kontoplan in kontoplans)
                {
                    string lockkonto = kontoplan.LockKonto.ToString();
                    if (lockkonto == "Locked")
                    {
                <tr style="background-color:skyblue">
                    <td><input type="checkbox" checked /></td>
                    <td>@kontoplan.Kontonummer</td>
                    <td>@kontoplan.Navn</td>
                    <td>@kontoplan.Type</td>
                    <td>@kontoplan.Moms</td>
                    <td>@kontoplan.LockKonto</td>
                    <td>@kontoplan.KlientId</td>
                </tr>
                    }
                    else
                    {
            <tr>
                <td><input type="checkbox" /></td>
                <td>@kontoplan.Kontonummer</td>
                <td>@kontoplan.Navn</td>
                <td>@kontoplan.Type</td>
                <td>@kontoplan.Moms</td>
                <td>@kontoplan.LockKonto</td>
                <td>@kontoplan.KlientId</td>
            </tr>
                    }
                }
            </tbody>
        </table>
    }
</EditForm>

@code {
    [Parameter]
    public int Id { get; set; }

    [Parameter]
    public int KlientId { get; set; }

    List<KontoplanModel> kontoplans = null;
    KlientGateway gateway = new KlientGateway();
    List<KlientModel> klienter = new List<KlientModel>();
    KlientModel klient = new KlientModel();
    KontoplanModel kpm = new KontoplanModel();


    protected override void OnInitialized()
    {
        klienter = gateway.GetAllKlient();
    }

    protected void OnInitializedKontoplan()
    {
        kontoplans = gateway.GetKontoplan(KlientId);
        KlientId = kpm.KlientId;
    }

public class KlientGateway
    {
        ApplicationDbContext _db = new ApplicationDbContext();

        public List<KlientModel>GetAllKlient()
        {
            return _db.Klient.ToList();
        }

        public List<KontoplanModel> GetKontoplan(int KlientId)
        {
            return _db.Kontoplan.ToList();
        }
    }

namespace BOGF.Models
{
    [Keyless]
    public class KontoplanModel
    {
        public int Kontonummer { get; set; }
        public string Navn { get; set; }
        public string Type { get; set; }
        public string Moms { get; set; }
        public string LockKonto { get; set; }
        public int KlientId { get; set; }
    }
}
Posted
Updated 27-Jul-21 23:55pm
v2

1 solution

This is normal as you do not use the KlientId parameter in the GetKontoplan method.
You must do it like this:
public List<KontoplanModel> GetKontoplan(int KlientId)
{
	return _db.Kontoplan.Where(x => x.KlientId == KlientId).ToList();
}
 
Share this answer
 
Comments
Member 14085040 28-Jul-21 8:24am    
@TheRealSteveJudge. Thanks for your reply. I have tried it, but nothing comes up in the table when I press the button!
TheRealSteveJudge 28-Jul-21 8:49am    
When you debug the GetKontoplan method after clicking the button what value is KlientId?
Member 14085040 28-Jul-21 9:14am    
I think the error is that I have in the Drop Down list both Id and Klient name from the Klient table from DB. And KlientId is from the Kontoplan table. Look at it:
@foreach (var klient in klienter)
{
@klient.Id- @klient.Name
}
I've tried removing @ klient.Name so it works.
How can we solve it?
TheRealSteveJudge 28-Jul-21 10:05am    
Try this
<option id="@klient.Id" name="@klient.Name" value="@klient.Id">@klient.Id - @klient.Name</option>
Member 14085040 28-Jul-21 13:25pm    
Yes it works now. Thank you very much for your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900