【问题标题】:How to display data from multiple tables/models in a single cshtml in asp.net core .NET6 (Razor pages)如何在 asp.net core .NET6(Razor 页面)中的单个 cshtml 中显示来自多个表/模型的数据
【发布时间】:2022-01-07 15:17:59
【问题描述】:

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

我需要在 Class-> create.cshtml 中显示 User_Profile(表/模型)(默认情况下,类附加到类表)。 以下是 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) 

非常感谢您的帮助。

谢谢!

【问题讨论】:

  • 您可以将多个类附加到一个创建视图的类。欢迎来到 S.O.请提供不在图像中的代码。进行搜索并提出一个具体的问题,而不是那么广泛。请阅读指南和如何提问的教程
  • 嗨@LeandroBardelli,我希望我的问题现在更清楚了。如果您需要更多信息,请告诉我。
  • 通常你会有一个带有应用程序/业务逻辑层的项目。该层将提供您的页面请求的数据。因此,正是在这一层中,您需要将不同的数据对象“粘合”在一起,以创建一个可以在视图中显示的类。希望这会有所帮助
  • 代码什么时候报错的?提交表单后还是直接运行代码?能否提供您的页面模型和错误信息?

标签: c# asp.net asp.net-core razor-pages


【解决方案1】:

型号

 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)
        { 
           //.......
        }
     
    }

【讨论】:

  • 嗨@Xinran,我收到了NullException。因为我需要从您在以下行中硬编码它们的数据库中获取值: public List up = new List() 我应该替换什么 new User_Profile() { FirstName ="Lucy", LastName = “B”,ProfileType="b",Status="YES" },与?
  • 你好@Reet,当你需要从数据库中获取值时,你可以使用Linq来替换硬码,我写硬码只是为了测试方便
  • 嗨@Reet,我添加了一些新代码,您可以使用这些新代码替换硬代码。
  • 非常感谢@Xinran !!它现在正在工作。
猜你喜欢
  • 2018-03-18
  • 2020-10-24
  • 2019-01-19
  • 2020-09-02
  • 2020-01-02
  • 2021-03-17
  • 2022-11-18
  • 1970-01-01
  • 2021-05-19
相关资源
最近更新 更多