我是 ASP.NET Core razor 頁面的新手。我需要在單個頁面上顯示來自多個表的資料。我只能從頁面訪問一個模型類。你能幫我弄清楚如何從一個頁面(cshtml)訪問多個表。
我需要在 Class-> create.cshtml 中顯示 User_Profile(表/模型)(默認情況下,Class 附加到 Class 表)。以下是來自 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)
我將衷心感謝您的幫助。
謝謝!
uj5u.com熱心網友回復:
模型
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 方法中接收它。

當你想從資料庫中獲取值時,你可以像這樣更改一些代碼:
資料庫背景關系
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)
{
//.......
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/372581.html
