-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathList.cshtml
209 lines (199 loc) · 7.64 KB
/
List.cshtml
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
@inherits Microsoft.VisualStudio.Web.CodeGeneration.Templating.RazorTemplateBase
@using Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore
@using System.Collections.Generic
@using System.Linq
@{
bool debugMode = false;
if (debugMode) {
@: -- Debug Mode Enabled --
@: Model.ViewName: @Model.ViewName
@: Model.ViewDataTypeShortName: @Model.ViewDataTypeShortName
@: Model.ViewDataTypeName: @Model.ViewDataTypeName
@: Model.ModelMetadata.EntitySetName: @Model.ModelMetadata.EntitySetName
@: ControllerName: @ControllerName()
@: Model.IsPartialView: @Model.IsPartialView
@: Model.IsPartialView: @Model.IsPartialView
@: Model.IsPartialView: @Model.IsPartialView
@: Model.IsPartialView: @Model.IsPartialView
foreach (var property in Model.GetType().GetProperties())
{
@: @property
}
}
if (Model.IsPartialView)
{
}
else if (Model.IsLayoutPageSelected)
{
@:@@{
@: ViewData["Title"] = "@Model.ViewName";
if (!string.IsNullOrEmpty(Model.LayoutPageFile))
{
@: Layout = "@Model.LayoutPageFile";
}
@:}
@:
@:<h1>@Model.ViewName</h1>
@:
}
else
{
@:@@{
@:Layout = null;
@:}
@:
@:<!DOCTYPE html>
@:
@:<html>
@:<head>
@:<meta name="viewport" content="width=device-width" />
@:<title>@Model.ViewName</title>
@:</head>
@:<body>
// PushIndent(" ");
}
@: <kendo-grid name="@(ControllerName())Grid" height="550" on-change="handleChange" selectable="true">
@:<columns>
// Template for columns
Dictionary<string, IPropertyMetadata> propertyLookup = ((IModelMetadata)Model.ModelMetadata).Properties.ToDictionary(x => x.PropertyName, x => x);
Dictionary<string, INavigationMetadata> navigationLookup = ((IModelMetadata)Model.ModelMetadata).Navigations.ToDictionary(x => x.AssociationPropertyName, x => x);
foreach (var item in Model.ModelMetadata.ModelType.GetProperties())
{
if (propertyLookup.TryGetValue(item.Name, out IPropertyMetadata property)
&& property.Scaffold && !property.IsForeignKey && !property.IsPrimaryKey)
{
@: <column field="@GetValueExpression(property)" title="@GetValueExpression(property)" />
}
else if (navigationLookup.TryGetValue(item.Name, out INavigationMetadata navigation))
{
@: <column field="@GetValueExpression(navigation)" title="@GetValueExpression(navigation)[email protected])" />
}
}
@:</columns>
@:<toolbar>
@: <toolbar-button name="excel"/>
@: <toolbar-button name="pdf"/>
@: <toolbar-button name="create" template="tbCreate"/>
@: <toolbar-button name="edit" template="tbEdit"/>
@: <toolbar-button name="details" template="tbDetails"/>
@: <toolbar-button name="delete" template="tbDelete"/>
@:</toolbar>
@:<excel all-pages="true" />
@:<groupable enabled="true" />
@:<sortable enabled="true" />
@:<pageable button-count="5" refresh="true" />
@:<filterable enabled="true" />
@:<datasource type="DataSourceTagHelperType.Ajax" page-size="20">
@: <schema>
@: <model>
@: <fields>
// Template for fields
foreach (var item in Model.ModelMetadata.ModelType.GetProperties())
{
if (propertyLookup.TryGetValue(item.Name, out IPropertyMetadata property)
&& property.Scaffold && !property.IsForeignKey && !property.IsPrimaryKey)
{
@: <field name="@GetValueExpression(property)" type="@GetFieldDataType(property)" />
}
else if (navigationLookup.TryGetValue(item.Name, out INavigationMetadata navigation))
{
@: <field name="@GetValueExpression(navigation)" type="@GetFieldDataType(property)" />
}
}
@: </fields>
@: </model>
@: </schema>
@: <transport>
@: <read url="@@Url.Action("@(ControllerName())_Read")" />
@: </transport>
@:</datasource>
@: </kendo-grid>
if (Model.IsPartialView)
{
@: <!-- Move this script to the Scripts section of the page -->
}
@: <script>
@: function tbCreate(data) {
@: return `<a id='tbCreate' href='${location.pathname}/create' class='k-button'>Create</a>`;
@: };
@: function tbDetails(data) {
@: return `<a id='tbDetails'>Details</a>`;
@: };
@: function tbEdit(data) {
@: return `<a id='tbEdit'>Edit</a>`;
@: };
@: function tbDelete(data) {
@: return `<a id='tbDelete'>Delete</a>`;
@: };
@: function handleChange(args) {
@: let enabled = this.select().length != 0;
@: toolBar.edit.enable(enabled);
@: toolBar.details.enable(enabled);
@: toolBar.del.enable(enabled);
@: if (enabled) {
@: let selectedId = this.dataItem(this.select()).@(GetPrimaryKeyName());
@: toolBar.edit.wrapper.attr("href", `${location.pathname}/edit/${selectedId}`);
@: toolBar.details.wrapper.attr("href", `${location.pathname}/details/${selectedId}`);
@: toolBar.del.wrapper.attr("href", `${location.pathname}/delete/${selectedId}`);
@: } else {
@: toolBar.edit.wrapper.attr("href", "#");
@: toolBar.details.wrapper.attr("href", "#");
@: toolBar.details.wrapper.attr("href", "#");
@: }
@: };
@: let toolBar;
@: $(function () {
@: // references to UI elements
@: toolBar = {
@: create: $("#tbCreate").kendoButton().data("kendoButton"),
@: edit: $("#tbEdit").kendoButton({ enable: false }).data("kendoButton"),
@: details: $("#tbDetails").kendoButton({ enable: false }).data("kendoButton"),
@: del: $("#tbDelete").kendoButton({ enable: false }).data("kendoButton")
@: };
@: });
@: </script>
if (!Model.IsPartialView && !Model.IsLayoutPageSelected)
{
//ClearIndent();
@:</body>
@:</html>
}
}
@*Code for template*@
@functions
{
string GetPrimaryKeyName() =>
(Model.ModelMetadata.PrimaryKeys != null && Model.ModelMetadata.PrimaryKeys.Length == 1)
? Model.ModelMetadata.PrimaryKeys[0].PropertyName
: null;
string GetValueExpression(IPropertyMetadata property) => property.PropertyName;
string GetValueExpression(INavigationMetadata navigation) => navigation.AssociationPropertyName;
string GetEnumerableTypeExpression(string typeName) => "IEnumerable<" + typeName + ">";
bool IsNumericType(IPropertyMetadata property)
{
var types = new [] {
"System.Byte",
"System.SByte",
"System.UInt16",
"System.UInt32",
"System.UInt64",
"System.Int16",
"System.Int32",
"System.Int64",
"System.Decimal",
"System.Double",
"System.Single"
};
return types.Any(property.TypeName.Contains);
}
bool IsCheckbox(IPropertyMetadata p) => p.TypeName.Contains("System.Boolean");
bool IsSelect(IPropertyMetadata p) => p.IsEnum && !p.IsEnumFlags;
bool IsDate(IPropertyMetadata p) => p.TypeName.Contains("System.DateTime");
string GetFieldDataType(IPropertyMetadata property) =>
property == null ? "object" :
IsNumericType(property) ? "number" :
IsDate(property) ? "date" :
"string";
//Best guess for controller name
string ControllerName() => Model.ModelMetadata.EntitySetName;
}