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

Creating a check for duplicate entries in blazor

$
0
0

I'm using .NET 6 to create a serverside interface where users can input numbers into a database. I have made functions that handle the entry into the database. However, due to restraints on the database, as well as personal plans for the interface, duplicate entries are not allowed.

Here is the main razor page for the interface itself:

@page "/list/add"@inherits AddNumberModel<h1>@Title Number</h1><p style="font-size:1vw;">No duplicate vehicle numbers.</p><EditForm Model="@veh" OnValidSubmit="SaveNumber"><DataAnnotationsValidator /><div class="mb-3"><label for="number" class="form-label">Number</label><div class="col-md-4"><!--Add minlength="1" maxlength="7" before 'Required'--><InputText class="form-control" @bind-Value="veh.Number" placeholder="Enter New Number Here." required /></div><ValidationMessage For="@(() => veh.Number)" /></div><div class="form-group"><button type="submit" class="btn btn-primary">Save</button><button type="button" class="btn btn-light" @onclick="Cancel">Cancel</button></div></EditForm>

And here is the cs page attached to it that handles user entries:

using Microsoft.AspNetCore.Components;using Fireserver.Data;using Fireserver.Models;namespace Fireserver.Pages{    public class AddNumberModel : ComponentBase    {        [Inject]        protected NumberService NumberService { get; set; } = null!;        [Inject]        public NavigationManager? UrlNavigationManager { get; set; }        [Parameter]        public string? vehID { get; set; }        protected string Title = "Add";        public Number veh = new();        protected async Task SaveNumber()        {            if (veh.Number != null)            {                await Task.Run(() =>                {                    NumberService.Edit(veh);                });            }                else {                 await Task.Run(() =>                {                    NumberService.Create(veh);                });            }            Cancel();        }        public void Cancel()        {            if (UrlNavigationManager != null)            {                UrlNavigationManager.NavigateTo("/");            }        }    }}

And the access layer that actually pushes the change:

using Microsoft.EntityFrameworkCore;using Fireserver.Interface;using Fireserver.Models;using Fireserver.Data;using System.Text.RegularExpressions;using System.ComponentModel.DataAnnotations;namespace Fireserver.DataAccess{    public class NumberDataAccessLayer : INumber    {        private NumberDBContext db;        public NumberDataAccessLayer(NumberDBContext _db)        {            db = _db;        }        public void AddNumber(Number number)        {            try            {                db.Numbers.Add(number);                db.SaveChanges();            }            catch            {                throw;            }        }            public void UpdateNumber(Number number)        {            try            {                db.Numbers.Find(Convert.ToString(number));                if (vehicle != null)                {                    db.Numbers.Add(number);                }                db.SaveChanges();            }            catch            {                throw;            }        }

I tried adding onto the simple 'IF' statement in the cs page to check for duplicates:

Changing the if statement above to compare it to the initial value:

if (veh.Number != null && veh.Number != vehID)

I tried different iterations of it in both the cs file as well as the access layer, and I believe that I'm on the right track but not using the right values or configuration.


Viewing all articles
Browse latest Browse all 4839

Trending Articles