我收到错误处理请求时发生未处理的异常
I got Error An unhandled exception occurred while processing the request
我刚开始学习 ASP.net Core,我遇到了这个错误:
我一直在网上搜索解决方案,但没有得到任何结果。谁能帮我?这是我的代码:
Details.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);
}
public IActionResult Details(int? Id)
{
var DataCustomer = _conn.tabel_customer.Where(c => c.CustomerId == Id).ToList();
return View(DataCustomer);
}
您的视图需要的类型是 model
,您在操作中 return 到视图的类型是 list
试试下面的代码:
public async Task<IActionResult> Details(int? Id)
{
var DataCustomer = await _conn.tabel_customer.FirstOrDefaultAsync(c => c.CustomerId == Id);
return View(DataCustomer);
}
您正在 return 查看对象列表,而视图需要一个对象。
您应该将您的控制器更改为这样的东西,更改为 return 单个对象:
public IActionResult Details(int? Id)
{
var DataCustomer = _conn.tabel_customer.Where(c => c.CustomerId == Id).FirstOrDefault();
return View(DataCustomer);
}
或将模型更改为 @model IEnumerable<SixthApp.Models.Customer>
并在视图中循环遍历它以显示您从控制器获得的所有对象....
我刚开始学习 ASP.net Core,我遇到了这个错误:
我一直在网上搜索解决方案,但没有得到任何结果。谁能帮我?这是我的代码:
Details.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);
}
public IActionResult Details(int? Id) { var DataCustomer = _conn.tabel_customer.Where(c => c.CustomerId == Id).ToList(); return View(DataCustomer); }
您的视图需要的类型是 model
,您在操作中 return 到视图的类型是 list
试试下面的代码:
public async Task<IActionResult> Details(int? Id)
{
var DataCustomer = await _conn.tabel_customer.FirstOrDefaultAsync(c => c.CustomerId == Id);
return View(DataCustomer);
}
您正在 return 查看对象列表,而视图需要一个对象。
您应该将您的控制器更改为这样的东西,更改为 return 单个对象:
public IActionResult Details(int? Id)
{
var DataCustomer = _conn.tabel_customer.Where(c => c.CustomerId == Id).FirstOrDefault();
return View(DataCustomer);
}
或将模型更改为 @model IEnumerable<SixthApp.Models.Customer>
并在视图中循环遍历它以显示您从控制器获得的所有对象....