1

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>&nbsp;</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.

1 Answer 1

0

@model expects a certain type,@model Admin_IndexModel<T> is not supported,here's an issue related in asp.net mvc

For Asp.net Core Razor Pages,if you want to avoid avoid code duplication,you could create different partial views(Razor View not Razor Page )

A minimal example:

public class MyPageModel : PageModel
{


    [BindProperty(SupportsGet =true)]
    public string? Entity { get; set; }

    public List<BtnMsgs> btnMsgs { get; set; } = default!;

    public List<BtnMsgs> Chosen { get; set; } = default!;

    public List<BtnMsgs> GameRosters { get; set; } = default!;

    public void OnGet()
    {
       //read target entities from db

    }
}

Page:

@page "/Admin/{Entity}"
@model MyPageModel
@{

}

@if (some condition)
{
    <partial name="@Model.Entity" model="the entities you read from db"></partial>
}
// other conditions
....

And you could try with expression tree to reduce the codes in your partial view,I once tried a sample in this issue

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.