我遇到了错误的例外情况

发布于 2025-01-30 10:11:06 字数 1928 浏览 3 评论 0原文

我是新学习的asp.net core,我得到了一个错误:

”在此处输入图像描述

我一直在搜索解决方案i Internet,但没有得到任何结果。谁能帮我吗?这是我的代码:

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);
        }

i'm newly learning ASP.net Core and i got this error :

enter image description here

I have been searching the solution i internet but didn't get any result. Can anyone help me? this is my code:

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>

this is detail method in CustomerController.cs

        public IActionResult Details(int? Id)
        {
            var DataCustomer = _conn.tabel_customer.Where(c => c.CustomerId == Id).ToList();

            return View(DataCustomer);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃颗糖壮壮胆 2025-02-06 10:11:06
 公共iRinctionResult详细信息(int?id)
    {
        var dataCustomer = _conn.tabel_customer.Where(c =&gt; c.customerid == id).tolist();

        返回视图(DataCustomer);
    }
 

您的视图需求是模型,您在操作中返回视图的内容是list

尝试以下代码:

    public async Task<IActionResult> Details(int? Id)
    {
        var DataCustomer =  await _conn.tabel_customer.FirstOrDefaultAsync(c => c.CustomerId == Id);

        return View(DataCustomer);
    }
    public IActionResult Details(int? Id)
    {
        var DataCustomer = _conn.tabel_customer.Where(c => c.CustomerId == Id).ToList();

        return View(DataCustomer);
    }

The type your view needs is a model, and what you return to the view in your action is a list

Try below code:

    public async Task<IActionResult> Details(int? Id)
    {
        var DataCustomer =  await _conn.tabel_customer.FirstOrDefaultAsync(c => c.CustomerId == Id);

        return View(DataCustomer);
    }
悲念泪 2025-02-06 10:11:06

您正在返回对象列表,并查看期望一个对象。
您应该,要么将控制器更改为这样的东西,要返回单个对象:

public IActionResult Details(int? Id)
{
    var DataCustomer = _conn.tabel_customer.Where(c => c.CustomerId == Id).FirstOrDefault();

    return View(DataCustomer);
}

或将模型更改为@model iEnumerable&lt; sixthapp.models.models.customer&gt;,并在视图上循环槽以显示所有对象从控制器...

You are returning list of objects, and view expects a single object.
You should, either change your controller to something like this, to return single object:

public IActionResult Details(int? Id)
{
    var DataCustomer = _conn.tabel_customer.Where(c => c.CustomerId == Id).FirstOrDefault();

    return View(DataCustomer);
}

or change model to @model IEnumerable<SixthApp.Models.Customer> and loop trough it on a view to show all objects you got from controller....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文