Reputation: 12684
Using a form (with Html.BeginForm
), I have an input
with type number
. When I submit it like so:
I get the following error:
The parameters dictionary contains a null entry for parameter 'AmountOnHand' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Ingredient(Int32, System.String, Int32, System.String)' in 'BrewReport.Web.Controllers.AdminController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
The form (simplified) is as follows:
@using (Html.BeginForm("Ingredient", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input id="AmountOnHand" type="number" class="form-control" min="0" required="required" />
<input type="submit" class="btn btn-primary btn-lg" value="Save Changes to Ingredient" />
}
Here is the controller method:
[HttpPost]
public ActionResult CreateIngredient(string ingredientName, decimal amountOnHand, string measurementUnit)
{
IngredientsData.SaveIngredient(ingredientName, amountOnHand, measurementUnit);
return RedirectToAction("ManageIngredients");
}
Upvotes: 1
Views: 1892
Reputation: 14318
Your input field is missing the name
attribute, so when it gets posted back to the server, there is no value supplied and your code breaks.
You can fix this using one of the following methods:
Add the name
attribute to your input field.
<input id="AmountOnHand" name="AmountOnHand" type="number" class="form-control" min="0" required="required" />
Use the Html Helper to generate your field markup
@Html.TextBoxFor(x => x.AmountOnHand, new { @type="number", @class="form-control", min="0", required="required" });
This will also add the name attribute for you, and has the added benefit of being strongly typed, in case you change the property name in future.
Upvotes: 6