Kaynağa Gözat

Basic form example with complex model

Lukas Angerer 1 yıl önce
ebeveyn
işleme
364f1c1558

+ 18 - 7
Components/Pages/Counter.razor

@@ -8,12 +8,23 @@
 
 <button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
 
-@code {
-    private int currentCount = 0;
+<hr />
 
-    private void IncrementCount()
-    {
-        currentCount++;
-    }
+<h1>The Form</h1>
 
-}
+<EditForm Model="@_model" OnValidSubmit="OnValidSubmit" OnInvalidSubmit="OnInvalidSubmit">
+    <div>
+        <p>@_formMessage</p>
+    </div>
+    <DataAnnotationsValidator />
+    <ValidationSummary />
+    <div>
+        <label>
+            Identifier:
+            <InputText @bind-Value="_model.User.Code"/>
+        </label>
+    </div>
+    <div>
+        <button type="submit">Submit</button>
+    </div>
+</EditForm>

+ 38 - 0
Components/Pages/Counter.razor.cs

@@ -0,0 +1,38 @@
+using BlazorEditForms.Model;
+
+namespace BlazorEditForms.Components.Pages;
+
+public partial class Counter
+{
+    private int currentCount = 0;
+    private string _formMessage = string.Empty;
+    private Aggregate? _model;
+
+    public Counter()
+    {
+        _model = new Aggregate(
+            new Employee
+            {
+                Code = "TEST",
+                PhoneNumbers =
+                    [new PhoneNumber { Number = "01189998819991197253", Type = PhoneNumberType.Landline }]
+            },
+            new User()
+        );
+    }
+
+    private void IncrementCount()
+    {
+        currentCount++;
+    }
+
+    private void OnValidSubmit()
+    {
+        _formMessage = $"Last Submit: {DateTimeOffset.Now.TimeOfDay} was VALID";
+    }
+
+    private void OnInvalidSubmit()
+    {
+        _formMessage = $"Last Submit: {DateTimeOffset.Now.TimeOfDay} was INVALID";
+    }
+}

+ 13 - 0
Model/Aggregate.cs

@@ -0,0 +1,13 @@
+namespace BlazorEditForms.Model;
+
+public class Aggregate
+{
+    public Aggregate(Employee employee, User? user)
+    {
+        Employee = employee;
+        User = user;
+    }
+
+    public Employee Employee { get; set; }
+    public User? User { get; set; }
+}

+ 13 - 0
Model/Employee.cs

@@ -0,0 +1,13 @@
+using System.ComponentModel.DataAnnotations;
+
+namespace BlazorEditForms.Model;
+
+public class Employee
+{
+    [Required]
+    [MinLength(3)]
+    public string Code { get; set; } = string.Empty;
+    public string Firstname { get; set; } = string.Empty;
+    public string Lastname { get; set; } = string.Empty;
+    public IList<PhoneNumber> PhoneNumbers { get; set; } = [];
+}

+ 10 - 0
Model/PhoneNumber.cs

@@ -0,0 +1,10 @@
+using System.ComponentModel.DataAnnotations;
+
+namespace BlazorEditForms.Model;
+
+public class PhoneNumber
+{
+    [Phone]
+    public string Number { get; set; } = string.Empty;
+    public PhoneNumberType Type { get; set; }
+}

+ 7 - 0
Model/PhoneNumberType.cs

@@ -0,0 +1,7 @@
+namespace BlazorEditForms.Model;
+
+public enum PhoneNumberType
+{
+    Primary,
+    Landline,
+}

+ 13 - 0
Model/User.cs

@@ -0,0 +1,13 @@
+using System.ComponentModel.DataAnnotations;
+
+namespace BlazorEditForms.Model;
+
+public class User
+{
+    [Required]
+    [EmailAddress]
+    public string UserId { get; set; } = string.Empty;
+    [Required]
+    [MinLength(3)]
+    public string Code { get; set; } = string.Empty;
+}

+ 0 - 24
Properties/launchSettings.json

@@ -1,23 +1,6 @@
 {
   "$schema": "http://json.schemastore.org/launchsettings.json",
-    "iisSettings": {
-      "windowsAuthentication": false,
-      "anonymousAuthentication": true,
-      "iisExpress": {
-        "applicationUrl": "http://localhost:41744",
-        "sslPort": 44334
-      }
-    },
     "profiles": {
-      "http": {
-        "commandName": "Project",
-        "dotnetRunMessages": true,
-        "launchBrowser": true,
-        "applicationUrl": "http://localhost:5064",
-        "environmentVariables": {
-          "ASPNETCORE_ENVIRONMENT": "Development"
-        }
-      },
       "https": {
         "commandName": "Project",
         "dotnetRunMessages": true,
@@ -26,13 +9,6 @@
         "environmentVariables": {
           "ASPNETCORE_ENVIRONMENT": "Development"
         }
-      },
-      "IIS Express": {
-        "commandName": "IISExpress",
-        "launchBrowser": true,
-        "environmentVariables": {
-          "ASPNETCORE_ENVIRONMENT": "Development"
-        }
       }
     }
   }