473,503 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I add code that will show if a company exists? This should only be displayed as a message on the screen.

2 New Member
Expand|Select|Wrap|Line Numbers
  1. User.cs:
  2. [Table("Users")]
  3.     public class User
  4.     {
  5.         [Display(AutoGenerateField = false)]
  6.         public int UserId { get; set; }
  7.  
  8.         [Display(Name = "UserName")]
  9.         [Required(ErrorMessage = "UserName is required.")]
  10.         public string UserName { get; set; }
  11.  
  12.         [Display(Name = "Password")]
  13.         [Required]
  14.         [MinLength(8, ErrorMessage = "password must be atleast 8 characters")]
  15.         [DataType(DataType.Password)]
  16.         public string Password { get; set; }
  17.  
  18.         [Display(Name = "Email")]
  19.         [Required(ErrorMessage = "Email is required.")]
  20.         public string Email { get; set; }
  21.  
  22.         [Display(Name = "Company")]
  23.         [StringLength(255)]
  24.         [Required(ErrorMessage = "Company is required.")]
  25.         [Remote("doesCompanyExist", "Company", HttpMethod = "POST", ErrorMessage = "Company already exists. Please enter a different company.")]
  26.         public string Company { get; set; }
  27.  
  28.         public User GetRegisteredUser()
  29.         {
  30.             return new User
  31.             {
  32.                 UserName = UserName,
  33.                 Password = Password,
  34.                 Email = Email,
  35.                 Company = Company,
  36.  
  37.             };
  38.         }     
  39.  
  40.     }
  41.  
  42. AppDbContext.cs:
  43. public class AppDbContext : DbContext
  44.     {
  45.         public AppDbContext()
  46.         {
  47.  
  48.         }
  49.  
  50.         public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
  51.         {
  52.         }
  53.  
  54.         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  55.         {
  56.             if (!optionsBuilder.IsConfigured)
  57.             {
  58.                 optionsBuilder.UseSqlServer("Data Source=(LocalDb)\\MSSQLLocalDB;Initial Catalog=testblazor;Integrated Security=True;");
  59.             }
  60.         }
  61.  
  62.         //protected override void OnModelCreating(ModelBuilder modelBuilder)
  63.         //{
  64.         //    base.OnModelCreating(modelBuilder);
  65.         //    modelBuilder.Entity<User>()
  66.         //}
  67.  
  68.         public DbSet<User> User { get; set; }
  69.         public DbSet<Item> Items { get; set; }
  70.  
  71.     }
  72.  
  73. Register.razor:
  74. @page "/register"
  75.  
  76. @using TestBlazor.Models
  77.  
  78. <br />
  79. <br />
  80.  
  81. <h3><b>Register</b></h3>
  82. <br />
  83.  
  84. <EditForm class="needs-validation" Model="@_user" OnValidSubmit="@HandleValidSubmit" OnInvalidSubmit="@HandleInvalidSubmit">
  85.     <div class="alert @StatusClass">@StatusMessage</div>
  86.     <DataAnnotationsValidator />
  87.     <ValidationSummary />
  88.     <div class="form-group">
  89.         <p><b>User name</b></p>
  90.         <input id="username" class="solid" name="username" placeholder="Your username.." @bind-value="_user.UserName" />
  91.         <ValidationMessage For="@(() => @_user.UserName)"></ValidationMessage>
  92.     </div>
  93.     <div class="form-group">
  94.         <p><b>Password</b></p>
  95.         <input type="password" class="solid" id="password" placeholder="Your password.." @bind-value="_user.Password" />
  96.         <ValidationMessage For="@(() => @_user.Password)"></ValidationMessage>
  97.     </div>
  98.     <div class="form-group">
  99.         <p><b>Email</b></p>
  100.         <input id="email" class="solid" placeholder="you@example.com" @bind-value="_user.Email" />
  101.         <ValidationMessage For="@(() => @_user.Email)"></ValidationMessage>
  102.     </div>
  103.     <div class="form-group">
  104.         <p><b>Company</b></p>
  105.         <input id="company" class="solid" placeholder="Your company.." @bind-value="_user.Company" />
  106.         <ValidationMessage For="@(() => @_user.Company)"></ValidationMessage>
  107.     </div>
  108.  
  109.  
  110.     <br />
  111.  
  112.     <button disabled="@loading" class="btn btn-primary">
  113.  
  114.         @if (loading)
  115.         {
  116.             <span class="spinner-border spinner-border-sm mr-1"></span>
  117.             <NavLink href="/login" class="btn btn-link">Register</NavLink>
  118.         }
  119.         Register
  120.     </button>
  121.     <NavLink href="/login" class="btn btn-link">Login</NavLink>
  122. </EditForm>
  123.  
  124.  
  125.  
  126. @code {
  127.     private User _user = new User();
  128.  
  129.     private string StatusMessage;
  130.     private string StatusClass;
  131.  
  132.     private bool loading;
  133.  
  134.  
  135.     private void OnValidSubmit()
  136.     {
  137.         if (loading == true)
  138.         {
  139.             Console.WriteLine("You have successfully registered!");
  140.         }
  141.  
  142.         else
  143.         {
  144.             loading = false;
  145.             Console.WriteLine("Check your information again!");
  146.         }
  147.     }
  148.  
  149.     protected void HandleValidSubmit()
  150.     {
  151.         StatusClass = "alert-info";
  152.         StatusMessage = " You have successfully registered! Please click the Login button to log in!";
  153.     }
  154.  
  155.     protected void HandleInvalidSubmit()
  156.     {
  157.         StatusClass = "alert-danger";
  158.         StatusMessage = " Check your information again!";
  159.     }
  160.  
  161.  
  162.     public bool doesCompanyExist(string Company)
  163.     {
  164.         try
  165.         {
  166.  
  167.             if (Company != null)
  168.             {
  169.                 return true;
  170.             }
  171.  
  172.         }
  173.         catch (Exception)
  174.         {
  175.             return false;
  176.         }
  177.  
  178.         return false;
  179.  
  180.     }
  181.  
  182. }
  183.  
Jun 30 '21 #1
0 1835

Sign in to post your reply or Sign up for a free account.

Similar topics

2
996
by: Supra | last post by:
this code will worked. i can connected to any servers...but how do i added method called doColor from usercontrol to module Sub DisplayMessage(ByVal rtb As RichTextBox, ByVal sText As String,...
3
11145
by: ABC | last post by:
I want click a button and than to popup window which will show my one PDF file. How should I to coding in C#?
2
2086
by: Rhys.Mataira | last post by:
My code will not show up anything at all on the screen can someone guide me why this would be? <?php $pagetitle="Login"; If (!$_POST) or (!$_POST) { echo" <head></head> <style...
8
2729
by: Franky | last post by:
In a prior post I asked about referencing the folder "My Documents" and someone was good enough to tell me about: SHGetFolderPath(NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL, 0, szMyDocPath);...
1
1860
by: Santosh26 | last post by:
How can we show a window task bar message from Asp.Net , like when we receive any mail in our mail, it shows a flashy message
1
1636
by: jobin petyer | last post by:
The following recursive code will cause a stack overflow if the array list is too large. How can you fix this and still retain the recursive pattern? var list = readHugeList(); var nextListItem...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7006
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5592
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5021
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4685
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.