Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Value remains null when I select a dropdown in Blazor

$
0
0
<div class="form-floating mb-3"><select class="form-control" @bind="Input.SelectedGradeId"><option value="">Select Grade</option>        @if (Input?.Grades != null)        {            @foreach (var grade in Input.Grades)            {<option value="@grade.Id">                    @grade.Id - @grade.Level</option>            }        }</select><label for="grade" class="form-label">Grade</label><ValidationMessage For="() => Input.SelectedGradeId" class="text-danger" /><p>selected grade = @Input.SelectedGradeId</p></div><div class="form-floating mb-3"><InputSelect class="form-control" @bind-Value="Input.SelectedOrgUnitId"><option value="">Select Org Unit</option>        @if (Input?.OrgUnits != null)        {            @foreach (var orgUnit in Input.OrgUnits)            {<option value="@orgUnit.Id">                    @orgUnit.Id - @orgUnit.UnitName</option>            }        }</InputSelect><label for="org-unit" class="form-label">Org Unit</label><ValidationMessage For="() => Input.SelectedOrgUnitId" class="text-danger" /><p>selected org.unit = @Input.SelectedOrgUnitId</p></div>

when i select a dropdown the value is remain null which cause the form cannot be submitted.

private sealed class InputModel{    [Required]    [Display(Name = "Grade")]    public int SelectedGradeId { get; set; }    [Required]    [Display(Name = "Organization/Unit")]    public int SelectedOrgUnitId { get; set; }    public List<Grade> Grades { get; set; } = new List<Grade>();    public List<OrgUnit> OrgUnits { get; set; } = new List<OrgUnit>();}

this is the model

when i try to submit it for the first time it says there's problem with the foreign key but when i tried to insert the value manually from ssms it is successfully updated so its certain theres no problem within the database relation.but when i add a validator below:

private async Task OnValidSubmitAsync(){    Console.WriteLine($"Selected Grade ID: {Input.SelectedGradeId}");    Console.WriteLine($"Selected Org Unit ID: {Input.SelectedOrgUnitId}");    // Validation against the database    var selectedGrade = await DbContext.Grades.FindAsync(Input.SelectedGradeId);    var selectedOrgUnit = await DbContext.OrgUnit.FindAsync(Input.SelectedOrgUnitId);    if (selectedGrade == null)    {        Console.WriteLine("Invalid Grade selected.");        RedirectManager.RedirectToCurrentPageWithStatus("Error: Invalid Grade selected.", HttpContext);        return;    }    if (selectedOrgUnit == null)    {        Console.WriteLine("Invalid Org Unit selected.");        RedirectManager.RedirectToCurrentPageWithStatus("Error: Invalid Org Unit selected.", HttpContext);        return;    }    // Update user profile    user.GradeId = Input.SelectedGradeId;    user.OrgUnitId = Input.SelectedOrgUnitId;    var updateResult = await UserManager.UpdateAsync(user);    if (!updateResult.Succeeded)    {        RedirectManager.RedirectToCurrentPageWithStatus("Error: Failed to update profile.", HttpContext);        return;    }    await SignInManager.RefreshSignInAsync(user);    RedirectManager.RedirectToCurrentPageWithStatus("Your profile has been updated", HttpContext);}

it said that i cant submit it because the selected item is invalid, so it maybe because the returned value is null.i've try to use @onchange, @bind-Value, InputSelect component but it still doesnt work. and i've ensure that the data type is accordant.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>