【发布时间】: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