Hello Team,
in my order page, I'm able to save the data into the databse correctly but when i tried to pass the OrderNuMber and OrderId to the receipt view page and print the receipt after saving order I keep getting this error, any help will be appreciated.
The parameters dictionary contains a null entry for parameter 'orderId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult PrintSalesReceipts(Int32)' in 'SmartRestaurantManagement.Controllers.HomeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters

Ck NitinRank #189 · 12.9K reputationPosted Jul 2, 2025, 11:37 PM EDT
Means:
Your PrintSalesReceipts(int orderId) action expects a non-null
orderId(anintis non-nullable).But no value (or a
null) is being passed fororderIdin the URL or route data.The MVC model binder can’t assign
nullto a non-nullableint.Root causes
Here are some common reasons for this error:
The route or query string didn’t provide a value for
orderId.Example:
/Home/PrintSalesReceipts ? No orderId in URLinstead of
Solutions
Make
orderIdnullablepublic ActionResult PrintSalesReceipts(int? orderId) { if (!orderId.HasValue) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Order ID is required."); } // Proceed with orderId.Value ... }Harold Kelley15 reputationPosted Jul 2, 2025, 11:43 PM EDT
It looks like the error you're facing is due to the fact that the
PrintSalesReceipts(int orderId)method requires a non-nullableint, but you’re passingnullor not passingorderIdat all when calling that action @Space WavesEmmmanuel FIADUFERank #947 · 2.2K reputationPosted Jul 3, 2025, 11:31 AM EDT
Hello, you guys are right, I did that modification and it look like the receipt page view is not receiving the Order number generated to print the order so due to that it giving a null error, which I'm having trouble in fixing it
Tuhin PaulRank #7 · 71.9K reputationPosted Jul 3, 2025, 11:46 PM EDT
The error you are encountering occurs because the
PrintSalesReceiptsaction method in yourHomeControlleris expecting a non-nullable integer (int) parameter namedorderId, but the value being passed to it isnull. Sinceintis a value type and cannot acceptnull, the framework throws this exception. YourPrintSalesReceiptsmethod likely looks like this:public ActionResult PrintSalesReceipts(int orderId) { // Logic to generate and return the receipt }Here,
orderIdis a required parameter of typeint. If no value is provided fororderIdor if the value isnull, the framework cannot bind it to theintparameter.PrintSalesReceiptsaction (e.g., via a redirect or URL), theorderIdparameter is either missing or not properly passed.orderIdduring redirection after saving the order.The ASP.NET MVC framework attempts to bind route values, query strings, or form data to the action method parameters. If the binding fails (e.g.,
orderIdisnullor missing), it throws the error you are seeing.Save the Order
[HttpPost] public ActionResult SaveOrder(OrderModel order) { if (ModelState.IsValid) { // Save the order to the database var savedOrder = _orderService.Save(order); // Redirect to the PrintSalesReceipts action with the orderId return RedirectToAction("PrintSalesReceipts", new { orderId = savedOrder.Id }); } // If the model is invalid, return to the form return View(order); }Print the Receipt
[HttpGet] public ActionResult PrintSalesReceipts(int orderId) { // Fetch the order details from the database var orderDetails = _orderService.GetOrderById(orderId); if (orderDetails == null) { return HttpNotFound("Order not found."); } // Pass the order details to the view return View(orderDetails); }View (PrintSalesReceipts.cshtml):
orderIdparameter.orderIdis included.Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Jul 4, 2025, 12:43 PM EDT
Thank you Mr Paul.
For the route, I created POSController for the order view page and the receipt view page, the HomeController maintain the default route, and also the system consist of two logins details such as the admin page for the dashboard side and POS system for the sales and printing of receipts.
I follow your steps alright but the OrderNumber, and OrderDate keep catching this NullReferenceException error and also says; Object reference not set to an instance of an object.
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Jul 4, 2025, 12:50 PM EDT
The OrderNumber is generated within the CreatePOS view page and order details are pass from the POSController to the OrderRepository to the database.
Additional help will be highly appreciated.
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Jul 4, 2025, 12:54 PM EDT
The array of the data always get saves perfectly into the database but the error occurs on the receipt view page, and because it is not able to pass the data to the receipt.
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Jul 4, 2025, 1:51 PM EDT
This the OrderId value in the URL
On the Receipt View Page
@model SmartRestaurantManagement.ViewModel.OrderViewModel
Receipt #: @(Model.OrderNumber ?? "N/A")
Date/Time: @(Model.OrderDate.ToString("yyyy-MM-dd HH:mm") ?? "N/A")
Cashier: @(ViewBag.UserId ?? "Unknown")
An exception of type 'System.NullReferenceException' occurred in App_Web_4doslt4g.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object.
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Jul 7, 2025, 3:49 AM EDT
Hello team,
With reference to my earlier post with the issues of the OrderNumber and the OrderDate catching null reference error on the receipt view page, any additional information will highly appreciated.