Html.Actionlink没有向Controller提供这两个参数 - 可以为null的int异常

时间:2017-03-23 15:32:07

标签: c# asp.net-mvc razor controller actionlink

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

我的行动链接是:

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

然而,当点击动作链接时,url onl提供第一个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)

这也行不通。

2 个答案:

答案 0 :(得分:0)

这是我怎么做的: 首先删除此dataAnotation [Route(" {customerId} / {id}")] 然后当你调用Html.ActionLink("新EB","创建","错误",新{id = 1,customerId = 24},htmlAttributes:空值) 链接看起来像: http://localhost:65339/Erroneous/Create?id=1&customerId=24 我希望我的回答能帮到你,祝你有个美好的一天!

答案 1 :(得分:0)

我们解决了这个问题。

我犯了一个愚蠢的错误并更改了错误的ActionLink,因此我们在用户界面中点击的那个被证明是完全错误的。

卫生署!