Question

I'm getting the dreaded "The parameters dictionary contains a null entry for parameter 'equipmentid' of non-nullable type" error when attempting to pass data through with a form. I've scoured google and stack overflow for answers and explanations, but I've come to the conclusion the problem has to me be not understanding how this stuff works. Coming from a traditional programming background the web stuff just doesn't seem as intuitive to me.

Here is my view code

@using (Html.BeginForm("CreateReservation", "ReservationList", FormMethod.Get, new { @equipmentID = 1 }))
{
  <button class="btn btn-blue">Click</button>
}

Here is my controller

public ActionResult CreateReservation(int equipmentid)
{
    // TODO: Query the piece of equipment being reserved.
    return View();
}

Can anyone tell me what exactly am I doing wrong? From the answers I've read before and the examples I've viewed, I feel as though this should work.

Was it helpful?

Solution

I am assuming from your code that this is an Asp.Net MVC project, using C# as the language.

There are a number of issues. The first is that the equipmentID variable is not being passed to the view. There are a number of ways to do this. The simplest will be to do it in your controller using the ViewData dictionary, as follows:

public ActionResult CreateReservation(int equipmentid)
{
    // TODO: Query the piece of equipment being reserved.

    ViewData["equipmentID"] = equipmentid;

    return View();
}

Once this is done, you could modify your view code to use the value as follows:

@using (Html.BeginForm("CreateReservation", "ReservationList", FormMethod.Get, new { equipmentID = ViewData["equipmentID"] }))
{
  <button class="btn btn-blue">Click</button>
}

This is fine for one or two values, but when the view gets more complex, you rather want to create a ViewModel class, and pass that class to your view.

Secondly - your use of the @ in front of equipmentID inside your BeginForm call indicates that you probably need to study up a bit on Razor syntax. A good place to start is Phil Haack's Razor Syntax Quick Reference. Razor syntax is what tells the parser which parts of your view are static HTML that it should output as is, and which parts are C# code that it should execute before sending to the browser. If you are using Visual Studio as your IDE, it is pretty good at highlighting which parts of your code are being interpreted as C# code, and which parts as ordinary HTML.

In my screenshot below you can see that VS2012 highlights C# code with a light grey background colour. The @ symbol is the start of C# code - so you don't need to use it again unless you break out of the C# block and go back to HTML.

Example of how Razor C# code is highlighted in Visual Studio 2012

And thirdly, you probably wouldn't want to pass the equipment ID as an HTML attribute inside your form tag. You should rather create a hidden form field inside your form body and set the name to equipmentID, and the value to the variable's value. The way you are currently using it will add an HTML attribute to the form tag, and your generated code will look something like this:

<form action="/ReservationList/CreateReservation" method="get" equipmentID="1">

And this extra attribute will not be retrievable in your code. Rather do something like this in your view:

@using (Html.BeginForm("CreateReservation", "ReservationList", FormMethod.Get, new { name = "myFormName", id = "myFormID" }))
{
    @Html.Hidden("equipmentID", ViewData["equipmentID"])
    <button class="btn btn-blue">Click</button>
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top