-
Notifications
You must be signed in to change notification settings - Fork 229
/
Create.tt
76 lines (70 loc) · 3.43 KB
/
Create.tt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<#@ template hostSpecific="true" linePragmas="false" #>
<#@ parameter type="Microsoft.DotNet.Tools.Scaffold.AspNet.Models.BlazorCrudModel" name="Model" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Linq" #>
<#
string modelName = Model.ModelInfo.ModelTypeName;
string pluralModel = Model.ModelInfo.ModelTypePluralName;
string modelNameLowerInv = modelName.ToLowerInvariant();
string pluralModelLowerInv = pluralModel.ToLowerInvariant();
string dbContextNamespace = string.IsNullOrEmpty(Model.DbContextInfo.DbContextNamespace) ? string.Empty : Model.DbContextInfo.DbContextNamespace;
string dbContextFullName = string.IsNullOrEmpty(dbContextNamespace) ? Model.DbContextInfo.DbContextClassName : $"{dbContextNamespace}.{Model.DbContextInfo.DbContextClassName}";
string dbContextFactory = $"IDbContextFactory<{dbContextFullName}> DbFactory";
string modelNamespace = Model.ModelInfo.ModelNamespace;
var entityProperties = Model.ModelInfo.ModelProperties
.Where(x => !x.Name.Equals(Model.ModelInfo.PrimaryKeyName, StringComparison.OrdinalIgnoreCase)).ToList();
#>
@page "/<#= pluralModelLowerInv #>/create"
@using Microsoft.EntityFrameworkCore
<#
if (!string.IsNullOrEmpty(modelNamespace))
{
#>@using <#= modelNamespace#>
<# }
#>
@inject <#=dbContextFactory #>
@inject NavigationManager NavigationManager
<PageTitle>Create</PageTitle>
<h1>Create</h1>
<h2><#= modelName #></h2>
<hr />
<div class="row">
<div class="col-md-4">
<EditForm method="post" Model="<#= modelName #>" OnValidSubmit="Add<#= modelName #>" FormName="create" Enhance>
<DataAnnotationsValidator />
<ValidationSummary class="text-danger" />
<#
foreach (var property in entityProperties)
{
string modelPropertyName = property.Name;
string modelPropertyNameLowercase = modelPropertyName.ToLowerInvariant();
string propertyShortTypeName = property.Type.ToDisplayString().Replace("?", string.Empty);
var inputTypeName = Model.GetInputType(propertyShortTypeName);
var inputClass = Model.GetInputClassType(propertyShortTypeName);
#>
<div class="mb-3">
<label for="<#= modelPropertyNameLowercase #>" class="form-label"><#= modelPropertyName #>:</label>
<<#= inputTypeName #> id="<#= modelPropertyNameLowercase #>" @bind-Value="<#= modelName #>.<#= modelPropertyName #>" class="<#= inputClass #>" />
<ValidationMessage For="() => <#= modelName #>.<#= modelPropertyName #>" class="text-danger" />
</div>
<# } #>
<button type="submit" class="btn btn-primary">Create</button>
</EditForm>
</div>
</div>
<div>
<a href="/<#= pluralModelLowerInv #>">Back to List</a>
</div>
@code {
[SupplyParameterFromForm]
private <#= modelName #> <#= modelName #> { get; set; } = new();
// To protect from overposting attacks, see https://learn.microsoft.com/aspnet/core/blazor/forms/#mitigate-overposting-attacks.
private async Task Add<#= modelName #>()
{
using var context = DbFactory.CreateDbContext();
context.<#= Model.DbContextInfo.EntitySetVariableName #>.Add(<#= modelName #>);
await context.SaveChangesAsync();
NavigationManager.NavigateTo("/<#= pluralModelLowerInv #>");
}
}