You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Tag Helpers失效:Edit/Delete按钮点击无响应求助

ASP.NET项目Edit/Delete按钮无响应问题

问题现象

  • 此前正常的Edit和Delete按钮,重启项目后点击无响应,但Create按钮可正常工作
  • Visual Studio中Create按钮的asp-controller与asp-action属性显示为绿色,Edit/Delete的相关属性无此高亮显示

页面代码

@model IEnumerable<Customer>
@{
    ViewData["Title"] = "Index";
}

<h2 class="display-5 mt-3 mb-1 fw-bold">Customer</h2>
<p class="mb-4">To home page</p>

<a asp-controller="Customer" asp-action="Create" class="btn btn-primary mb-2 mt-3">Create</a>

<table class="table table-bordered table-striped">
    <thead class="table-header bg-primary ">
        <tr>
            <th scope="col">
                Company Name
            </th>
            <th scope="col">
                Contact Person
            </th>
            <th scope="col">
                Email
            </th>
            <th scope="col">Phone</th>
            <th ">Active</th>
        </tr>
    </thead>
    @foreach (var obj in Model)
    {
        <tbody>
            <tr>
                <td>
                    @obj.CompanyName
                </td>
                <td>
                    @obj.ContactPerson
                </td>
                <td >
                    @obj.Email
                </td>
                <td >
                    @obj.PhoneNumber 
                </td>
                <td >
                    @obj.Active
                </td>
                <td>
                    <div class="w-75 btn-group" role="group">
                        <a asp-controller="Customer" asp-action="Edit" asp-route-id="@obj.Id"
                           class="btn btn-primary mx-2">Edit</a>
                        <a asp-controller="Customer" asp-action="Delete" asp-route-id="@obj.Id"
                           class="btn btn-danger mx-2">Delete</a>
                        <a href="https://getbootstrap.com/" class="bg-info">
                           is this a tag working</a>
                    </div>
                </td>
            </tr>
        </tbody>
    }
</table>

解决方案

  • 修复HTML语法错误:代码中<th ">Active</th>存在语法错误,多余的引号破坏了HTML结构与Razor标签助手的解析,修改为: <th scope="col">Active</th>
  • 调整表格结构:tbody标签不应嵌套在foreach循环内,一个表格仅需一个tbody,将循环移至tbody内部遍历行:
    <tbody>
        @foreach (var obj in Model)
        {
            <tr>
                <!-- 行内容 -->
            </tr>
        }
    </tbody>
    
  • 验证控制器方法:确认CustomerController中存在Edit(int id)和Delete(int id)的GET请求方法,路由配置正常
  • 清理重建项目:在Visual Studio中执行清理解决方案→重建解决方案,清除缓存后重新运行项目

内容的提问来源于stack exchange,提问作者Null Zero

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 23:24:57