Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select option default value of null is translated as string "null" when submitting form

With this select below the default value of null is working when I first load it and then when I change it's value to some other option, but then if I change it back to the default "None selected" and submit the value is converted to a string "null", so when I try and pass it to my controller where the controllers property is a int? (nullable int) it throws an exception because I'm try to pass in a string.

Is there a way to keep it as a null and not a string or should I be using all string values for the values?

FYI - I don't wan't to do any JS post submit processing to check the value

<div class="form-group col-md-4">
  <label for="processOptionId">Claim Process</label>
  <select class="form-control" [(ngModel)]="claim.data.processOptionId" name="processOptionId">
    <option value=null>None selected</option>
    <option *ngFor="let processOption of processOptionList" [value]="processOption.code">{{processOption.name}}</option>
  </select>
</div>
like image 404
user1186050 Avatar asked Dec 22 '25 03:12

user1186050


1 Answers

You should bind the null option value with [ngValue]:

<option [ngValue]="null">None selected</option>
like image 191
ConnorsFan Avatar answered Dec 23 '25 17:12

ConnorsFan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!