Html.Actionlink 没有向控制器提供两个参数 - 可为空的 int 异常

Html.Actionlink not providing both parameters to Controller - nullable int exception

我正在尝试将两个参数传递给控制器​​以允许进行简单的创建过程。

我的行动link是:

Html.ActionLink("New EB", "Create", "Erroneous", new { id = Model.Id, customerId = Model.Customer.Id }, htmlAttributes: null)

我的控制器是:

[Route("{customerId}/{id}")]
    public ActionResult Create(int id, int customerId)
    {
        if (customerId == 0)
        {
            return HttpNotFound();
        }

        var customer = _context.Customers.Single(c => c.Id == customerId);

        if (customer == null)
        {
            return HttpNotFound();
        }

        var viewModel = new CreateErroneousBillViewModel
        {
            Id = id,
            CustomerId = customer.Id,
        };

        return View("CreateErroneousBillForm", viewModel);
    }

然而,当点击操作 link 时,url 仅提供第一个 ID 而不是第二个 ID,如下所示:

http://localhost:65339/Erroneous/Create/24/

其中 24 是客户的 ID

而我认为它应该提供类似

的内容
http://localhost:65339/Erroneous/Create/24/1

其中 1 是 Id

感谢任何帮助,我已经为此绞尽脑汁了一段时间。

编辑:

我也试过设置绝对值来消除来自模型的空值

Html.ActionLink("New EB", "Create", "Erroneous", new { id = 1, customerId = 24 }, htmlAttributes: null)

这也没有用。

我会这样做: 首先删除这个 dataAnotation [Route("{customerId}/{id}")] 然后当你调用 Html.ActionLink("New EB", "Create", "Erroneous", new { id = 1, customerId = 24 }, htmlAttributes: null) link 看起来像: http://localhost:65339/Erroneous/Create?id=1&customerId=24 希望我的回答对您有所帮助,祝您有个愉快的一天!

我们解决了这个问题。

我犯了一个愚蠢的错误并更改了错误的 ActionLink,所以我们在 UI 中单击的链接结果完全是错误的。

哎呀!