如何在 asp.net 核心 .NET6(Razor 页面)中的单个 cshtml 中显示来自多个 tables/models 的数据

How to display data from multiple tables/models in a single cshtml in asp.net core .NET6 (Razor pages)

我是 asp.net 核心剃须刀页面的新手。我需要在单个页面上显示来自多个 table 的数据。我只能从页面访问一个模型 class。你能帮我弄清楚如何从一个页面 (cshtml) 访问多个 table 吗?

我需要在Class->create.cshtml中显示User_Profile(Table/model)(Class附加到Classtable 默认)。 以下是 Class -> create.cshtml

中的代码
 @page
@model TestProject.Pages.Class.CreateModel

@{
    ViewData["Title"] = "Create";
    Layout = "~/Pages/Shared/_Layout.cshtml";
}


<div class="row">
    <div class="col-md-4">
        <form method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                @*<label asp-for="Class.ID" class="control-label"></label>
                <input asp-for="Class.ID" class="form-control" />*@
                @*<label asp-for="User_Profile.ID" class="control-label"></label>
                <select asp-for="User_Profile.ID" class="form-control" >
                    <option value="">Select Student</option>
                    <option value="Female">Female</option>
                    <option value="Male">Male</option>
                </select>
                <span asp-validation-for="Class.ID" class="text-danger"></span>*@
            </div>
            <div class="form-group">
                <label asp-for="Class.Description" class="control-label"></label>
                <input asp-for="Class.Description" class="form-control" />
                <span asp-validation-for="Class.Description" class="text-danger"></span>
            </div>
           
            
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
            <table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.userprofile[0].FirstName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.userprofile[0].LastName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.userprofile[0].ProfileType)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.userprofile[0].Status)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model.User_Profile) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.FirstName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.LastName)
            </td>
            <td> 
                @Html.DisplayFor(modelItem => item.ProfileType)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Status)
            </td>
           
            
        </tr>
}
    </tbody>
</table>

        </form>
    </div>
</div>


@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

我在写 Model.User_profile 的地方遇到了错误。例如:

@foreach (var item in Model.User_Profile) 

非常感谢您的帮助。

谢谢!

型号

 public class Class
    {
        public int ID { get; set; }
        
        public string Description { get; set; }
    }

public class User_Profile
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string ProfileType { get; set; }
        public string Status { get; set; }
    }

页面模型

    public class CreateModel : PageModel
        {
            public Class cla { get; set; }
            public List<User_Profile> up = new List<User_Profile>()
                {
               //I write these hard-code just for testing convenience
                    new User_Profile()
                    {
                        FirstName ="mike",
                        LastName ="A",
                        ProfileType="a",
                        Status="aaa"
                    },
                    new User_Profile()
                    {
                        FirstName ="Lucy",
                        LastName ="B",
                        ProfileType="b",
                        Status="YES"
                    },
                    new User_Profile()
                    {
                        FirstName ="Nacy",
                        LastName ="C",
                        ProfileType="c",
                        Status="NO"
                    }
                }; 
    
    
            public void OnGet()
            {
                
            }
    
            public void OnPost(Class cla) 
            {
                
            }
        }

页面浏览量

<div class="row">
    <div class="col-md-4">
        <form method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="cla.Description" class="control-label"></label>
                <input asp-for="cla.Description" class="form-control" />
                <span asp-validation-for="cla.Description" class="text-danger"></span>
            </div>
           
            
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
            <table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.up[0].FirstName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.up[0].LastName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.up[0].ProfileType)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.up[0].Status)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model.up) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.FirstName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.LastName)
            </td>
            <td> 
                @Html.DisplayFor(modelItem => item.ProfileType)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Status)
            </td> 
        </tr>
}
    </tbody>
</table>

        </form>
    </div>
</div>


@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

页数

当您输入一些值并点击提交按钮时,您可以在Post方法中接收它。

当你想从数据库中获取值时,你可以这样修改一些代码:

DbContext

public class Pagecontext : DbContext
    {
        public Pagecontext(DbContextOptions<Pagecontext> options) : base(options)
        {

        }

        public DbSet<User_Profile> UpTable { get; set; }
        
    }

页面模型

public class CreateModelModel : PageModel
    {
        public readonly  Pagecontext _db;
        public CreateModelModel(Pagecontext db)
        {
            _db = db;
        }
        public Class cla { get; set; }
            
        public List<User_Profile> up{ get; set; }
        public async Task OnGetAsync()
        {
            up= await _db.UpTable.ToListAsync();
        }
        public async Task OnPostAsync(Class cla)
        { 
           //.......
        }
     
    }