ian
ian

Reputation: 113

Null Parameter Error

I am having a problem calling an Edit method in an application I am creating. In the view an ActionLink is clicked that should be passing the order number to the Edit method as a parameter and opening an edit page with the info for the order populated in the fields. However upon clicking the link I receive the error:

The parameters dictionary contains a null entry for parameter 'orderNum' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ViewResult Edit(Int32)' in 'AddressUpdater.WebUI.Controllers.OrderController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

However the parameter is present in the URL. Here are the relevant method:

public ViewResult Edit(int orderNum)
{
    Order order = repository.Orders.First(o => o.OrderNumber == orderNum);
    return View(order);
}

If if change the parameter to int? orderNum the page will render without an error but none of the data is there.

Upvotes: 0

Views: 1157

Answers (1)

Juri
Juri

Reputation: 32900

Most probably there's something wrong with the sending of the data to the action method Edit, i.e. in your action link. Just open some devtool like Firebug or Chrome Dev tools to inspect what is being sent to the server.

When your url looks like

Edit?OrderNumber=1234

then you need to have a matching parameter on your Action method like

public ViewResult Edit(int orderNumber) {...}

Instead

Edit(int orderNum){...}

won't work. Basically url parameter name and action method parameter name have to match (not case sensitive, but the name has to match)

Upvotes: 1

Related Questions