首页 > 解决方案 > asp-route-id:传递的整数变为0

问题描述

我有以下问题。我有一个产品,我对其进行了一些测试。在我的 cshtml 页面中,我展示了一些产品项目的测试运行,如下所示:

<td><a asp-action="TestDetail" asp-route-id="@Model.ResultViewModels[i].TestRunId">Content</a></td>

TestRunId 是一个整数。例如,TestRunId 可能是 9。这有效:URL 看起来正确。在我的 cshtml 页面中,用户可以点击它来查看更多详细信息。

但是,当我在控制器中记录 asp-route-id 时,这个 TestRunId 似乎“丢失”了。在下面的示例中,第二个日志记录显示testrunId: 0. 我希望这也是 9。

        public IActionResult TestPlanRunDetail(int TestRunId)
        {
            logger.LogInformation("TestPlanRunDetail function called.");
            logger.LogInformation("TestRunId: " + TestRunId);
            // Omitted for brevity
            return View(detail);
        }

标签: asp.netasp.net-mvcasp.net-core

解决方案


里面的参数名asp-route-xxx必须和后端的参数名一致(int xxx)

因此,更改您的参数名称之一以匹配另一个


推荐阅读