c# - MVC Number Input Being Treated as null -


using form (with html.beginform), have input type number. when submit so:

input type number

i following error:

the parameters dictionary contains null entry parameter 'amountonhand' of non-nullable type 'system.int32' method 'system.web.mvc.actionresult ingredient(int32, system.string, int32, system.string)' in 'brewreport.web.controllers.admincontroller'. optional parameter must reference type, nullable type, or declared optional parameter. parameter name: parameters

the form (simplified) 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 ingredient" /> } 

here controller method:

[httppost] public actionresult createingredient(string ingredientname, decimal amountonhand, string measurementunit) {     ingredientsdata.saveingredient(ingredientname, amountonhand, measurementunit);     return redirecttoaction("manageingredients"); } 

your input field missing name attribute, when gets posted server, there no value supplied , code breaks.

you can fix using 1 of following methods:

add name attribute input field.

<input id="amountonhand" name="amountonhand" type="number" class="form-control" min="0" required="required" /> 

use html helper generate field markup

@html.textboxfor(x => x.amountonhand, new { @type="number", @class="form-control", min="0", required="required" }); 

this add name attribute you, , has added benefit of being typed, in case change property name in future.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -