Search code examples
asp.net-mvcnullablerequest.form

Pass Nullable Int to Request.Form[] in mvc


I have submit form in View where price fields can be null. I get values in action with Request.Form[]:

int? min = int.Parse(Request.Form["min"])

but I get Exception when the field is null because null can't be parsed in int.

Anything else to try?


Solution

  • Test if the value is null before parsing

    int? min = null;
    if (Request.Form["min"] != null)
    {
      min = int.Parse(Request.Form["min"])
    }