I have the following database context class:
public partial class MainDB_Context : DbContext
{
public MainDB_Context()
{
}
public MainDB_Context(DbContextOptions<MainDB_Context> options)
: base(options)
{
}
public virtual DbSet<Chosen> Chosens { get; set; }
public virtual DbSet<BtnMsg> BtnMsgs { get; set; }
public virtual DbSet<GameRoster> GameRosters { get; set; }
...
}
and this generic page model:
public class Admin_IndexModel<T> : PageModel where T : class
{
readonly MainDB_Context dbContext;
readonly List<string> DbSetNames;
readonly DbSet<T> DbSet;
readonly List<PropertyInfo> DbSetPropInfo;
readonly List<string> DbSetPKeys;
public List<T> AsyncDbSetItems;
...
}
I want to create a single Razor page that will show tables from the database, for example for these URLs:
/Admin/Chosen
/Admin/BtnMsg
/Admin/GameRoster
I want @model type to be respectively:
Admin_IndexModel<Chosen>
Admin_IndexModel<BtnMsg>
Admin_IndexModel<GameRoster>
this is my current Razor page:
@page "{DbSetName?}"
@using System.Reflection;
@using asp_net_sql.Models
@using asp_net_sql.Pages
@model Admin_IndexModel<Chosen>
@{
var DbSetNames = ViewData["DbSetNames"] as List<string>;
var DbSetPropInfo = ViewData["DbSetPropInfo"] as List<PropertyInfo>;
...
}
<menu id="adminMenu">
@foreach (var menuItem in DbSetNames)
{
<div class="menu">@menuItem</div>
}
</menu>
<form method="post">
<table class="table">
<thead>
<tr>
@foreach (var pInf in DbSetPropInfo)
{
<th>@pInf.Name</th>
}
<th> </th>
</tr>
</thead>
<tbody>
@{
foreach (var item in Model.AsyncDbSetItems)
{
row++;
var btnId = $"btn_{row}";
<tr>
@foreach (var pInf in DbSetPropInfo)
{
var inpId = $"inp_{pInf.Name}_{row}";
<td>
<input id="@inpId" type="text" value="@pInf.GetValue(item)" />
</td>
}
<td>
<button id="@btnId"
type="submit"
asp-page-handler="Update">
Change (Post)
</button>
</td>
</tr>
}
}
</tbody>
</table>
</form>
how do I create expression for @model dynamically using DbSetName?
I don't have Views or Controllers.