Search code examples
c#asp.net-mvcrazorcontrolleractionlink

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


I am trying to pass two parameters into a controller to allow a simple Create process to happen.

My actionlink is:

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

My Controller is:

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

However when clicking on the action link the url onl provides the first id and not the second like such:

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

Where 24 is the Id of customer

whereas I think it should be providing something like

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

Where 1 is the Id

Any help appreciated, have been scratching my head over this for a while now.

Edit:

I have also tried setting absolute values to eliminate null values coming from the model

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

This also did not work.


Solution

  • We resolved this.

    I was making a silly error and changing the wrong ActionLink so the one that we were clicking in the UI turned out to be completely the wrong one.

    Doh!