我正在新學習 ASP.net Core,但出現此錯誤:

我一直在網上搜索解決方案,但沒有得到任何結果。誰能幫我?這是我的代碼:
詳細資訊.cshtml
@model SixthApp.Models.Customer
@{
ViewData["Title"] = "Details";
}
<h1>Details</h1>
<div>
<h4>Customer</h4>
<p>@ViewData["data"]</p>
<hr />
<dl class="row">
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.CustomerId)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.CustomerId)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Name)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Address)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Address)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.MobileNo)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.MobileNo)
</dd>
</dl>
</div>
<div>
@Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |
<a asp-action="Index">Back to List</a>
</div>
這是 CustomerController.cs 中的詳細方法
public IActionResult Details(int? Id)
{
var DataCustomer = _conn.tabel_customer.Where(c => c.CustomerId == Id).ToList();
return View(DataCustomer);
}
uj5u.com熱心網友回復:
public IActionResult Details(int? Id) { var DataCustomer = _conn.tabel_customer.Where(c => c.CustomerId == Id).ToList(); return View(DataCustomer); }
您的視圖需要的型別是 a model,而您在操作中回傳到視圖的型別是 alist
試試下面的代碼:
public async Task<IActionResult> Details(int? Id)
{
var DataCustomer = await _conn.tabel_customer.FirstOrDefaultAsync(c => c.CustomerId == Id);
return View(DataCustomer);
}
uj5u.com熱心網友回復:
您正在回傳物件串列,并且視圖需要一個物件。
您應該將您的控制器更改為這樣的東西,以回傳單個物件:
public IActionResult Details(int? Id)
{
var DataCustomer = _conn.tabel_customer.Where(c => c.CustomerId == Id).FirstOrDefault();
return View(DataCustomer);
}
或將模型更改為@model IEnumerable<SixthApp.Models.Customer>并在視圖上回圈遍歷它以顯示您從控制器獲得的所有物件......
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/479124.html
標籤:网 asp.net 核心 asp.net-mvc-4 剃刀 西装外套
