我試圖在一個視圖中顯示兩個2表。我不想把它顯示成一個串列,而是要顯示細節,而大多數教程中我得到的是一個串列視圖。
我嘗試了下面的視頻。 https://youtu.be/oN1f2Vpc-wU
現在我遇到的問題是,當我點擊鏈接打開ViewModel的詳細資訊頁面時,它顯示為空。
Model 1
[Table("UserRegistration")]
public partial class UserRegistration
{
[Key] 。
public Guid ClientId { get; set; }
[StringLength(50)]
public string FirstName { get; set; }
[StringLength(50)]
public string LastName { get; set; }
[StringLength(50)]
public string IdentityNumber { get; set; }
[StringLength(50)]
public string PhoneNumber { get; set; }
public string Password { get; set; }
public string ConfirmPassword { get; set; }
[Column(TypeName = "date")]
public DateTime? DateCreated { get; set; }
模型2
[Table("Voucher")]
public partial class Voucher
{
public int VoucherId { get; set; }
public Guid? CustomerId { get; set; }
[StringLength(50)]
public string HouseNumber { get; set; }
[StringLength(50)]
public string PostalCode { get; set; }
[StringLength(50)]
public string Location { get; set; }
[Column(TypeName = "date")]
public DateTime? DateCreated { get; set; }
ViewModel
public class ViewModel
{
public Guid? CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string IdentityNumber { get; set; }
public string HouseNumber { get; set; }
public string PostalCode { get; set; }
public string Location { get; set; }
Controller UserRegistration
private mymodel db = new mymodel()。
// GET: UserRegistrations
public ActionResult Index()
{
return View(db.UserRegistrations.ToList())。
控制器首頁
private mymodel db = new mymodel()。
//GET: 首頁
public ActionResult Index(Guid id)
{
//
UserRegistration ur = db.UserRegistrations.Single(x => x.ClientId == id)。
Voucher voucher = new Voucher()。
ViewModel viewm = new ViewModel()。
ur.FirstName = viewm.FirstName;
ur.LastName = viewm.LastName;
voucher.HouseNumber = viewm.HouseNumber;
voucher.Location = viewm.Location;
voucher.PostalCode = viewm.PostalCode。
return View(viewm)。
}
Index.cshtml
@model ExampleTestMVC.Models.ViewModel
@{
ViewBag.Title = "Index"/span>;
}
<h2>索引</h2>
<div>
<h4>用戶注冊</h4>
<hr />
<dl class="dl-horizontal"/span>>
<dt>
@Html.DisplayNameFor(model => model.FirstName)
</dt>
<dd>
@Html.DisplayFor(model => model.FirstName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.LastName)
</dt>
<dd>
@Html.DisplayFor(model => model.LastName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.HouseNumber)
</dt>
<dd>
@Html.DisplayFor(model => model.Location)
</dd>
<dt>
@Html.DisplayNameFor(model => model.PostalCode)
</dt>
<dd>
@Html.DisplayFor(model => model.PostalCode)
</dd>
</dl>
Index.chtml用于用戶注冊
@model IEnumerable< ExampleTestMVC.Models.UserRegistration>
@{
ViewBag.Title = "Index"/span>;
}
<h2>索引</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.IdentityNumber)
</th>
<th>
@Html.DisplayNameFor(model => model.PhoneNumber)
</th>
<th>
@Html.DisplayNameFor(model => model.Password)
</th>
<th>
@Html.DisplayNameFor(model => model.ConfirmPassword)
</th>
<th>
@Html.DisplayNameFor(model => model.DateCreated)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.IdentityNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.PhoneNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.Password)
</td>
<td>
@Html.DisplayFor(modelItem => item.ConfirmPassword)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateCreated)
</td>
<td>
@Html.ActionLink("一切細節", "索引", "首頁", new { id = item. ClientId }, null) |
</td>
</tr>
}
</table>
uj5u.com熱心網友回復:
你沒有為viewmodel設定值,而viewmodel是與細節視圖系結的。改變主頁控制器的代碼如下:
public ActionResult Index(Guid id)
{
//
UserRegistration ur = db.UserRegistrations.Single(x => x.ClientId == id)。
Voucher voucher = db.Vouchers.Single(x => x.CustomerId == id);
ViewModel viewm = new ViewModel()。
viewm.FirstName=ur.FirstName;
viewm.LastName=ur.LastName;
viewm.HouseNumber=voucher.HouseNumber;
viewm.Location=voucher.Location;
viewm.PostalCode=voucher.PostalCode。
return View(viewm)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/320362.html
標籤:
上一篇:配置了bundleconfig.json,但在ASP.NETCore3.1中沒有創建bundles。
下一篇:在C程式中使用一個匯編函式
