Skip to content

Cannot bind to nullable types (bool?, int?, etc) #5541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vertonghenb opened this issue Jun 15, 2018 · 16 comments
Closed

Cannot bind to nullable types (bool?, int?, etc) #5541

vertonghenb opened this issue Jun 15, 2018 · 16 comments
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@vertonghenb
Copy link
Contributor

A lot of work has been done for the bind method, let's add the one for nullable bool. bool?

Current error:
Uncaught (in promise) Error: System.ArgumentException: 'bind' does not accept values of type System.Nullable`1[[System.Boolean, mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]. To read and write this value type, wrap it in a property of type string with suitable getters and setters.

@vertonghenb
Copy link
Contributor Author

Will do a PR for this tomorrow

@SteveSandersonMS
Copy link
Member

What would it mean though? How would the UI state ever be “null”?

@vertonghenb
Copy link
Contributor Author

vertonghenb commented Jun 15, 2018

A nullable bool should show nothing, but it is usefull when you want to bind the value, like we're doing here If the checkbox is interacted with, it should be true or false but initially it should be nothing.

<p>
    Initially null checkbox:
    <input id="checkbox-initially-null" bind="checkboxInitialltyNullValue" type="checkbox" />
    <span id="checkbox-initially-null-value">@checkboxInitialltyNullValue</span>
</p>

@functions{
    bool? checkboxInitialltyNullValue = null;
}

@Andrzej-W
Copy link

Three-state checkbox demo:
https://demos.devexpress.com/aspnetcore-bootstrap/Editors-CheckBox#GrayedState
I believe we can create a similar Blazor component quite easily.

Here is the doc from Mozilla:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#Indeterminate_state_checkboxes

and some discussion from Vue:
vuejs/vue#4094

I have mixed feelings whether example code shown by @vertonghenb should work or not. Personally I prefer a real three-state checkbox component - it is much more user friendly.

@vertonghenb
Copy link
Contributor Author

@Andrzej-W
What I'm showing is a three state checkbox, which works if we add a little bit of code in the BindMethod.cs

  1. True,
  2. False,
  3. null

However, we're not really talking about the looks here rather the underlying coupling of the bind method

@Andrzej-W
Copy link

@vertonghenb Unfortunately I know almost nothing about Blazor internals yet, but I think it is not as simple as you wrote. Three-state checkbox should be able to map variable/property values to two HTML element properties:

variable value checked indeterminate
null any true
false false false
true true false

Can you achieve this effect by modifying only this one file (BindMethods.cs)?

@Andrzej-W
Copy link

I have just checked and nullable numeric types (int, decimal, etc.) are not supported either. For these types fix should be simpler. Null value can be interpreted as empty field.

@vertonghenb
Copy link
Contributor Author

So, will we allow nullables? If so i'll see if I can implement the others as well in #1012

  1. int?
  2. long?
  3. bool?
  4. float?
  5. double?
  6. decimal?

@Andrzej-W
Copy link

In my opinion it is a must have feature. Hopefully @SteveSandersonMS will agree.

@simonziegler
Copy link

Agree that this is a must have. I ended up creating a component for numeric input that handles nulls. When focus is gained, the field shows a raw number. In losing focus, the relevant number (checked with a regular expression) is written back in formatted form with commas and so on. If the field is empty on losing focus, the component interprets this as a null for a double? Datatype. Works reasonably well.

@faina09
Copy link

faina09 commented Oct 2, 2018

Version 0.6.0 seems still not supporting nullables (neither bool? nor the other 6 types listed by @vertonghenb) but this is a very important feature when using db!
@simonziegler can you please share your component for numeric inputs? I think we should work to insert nullables in Blazor!

@danroth27 danroth27 changed the title [Bind] Nullable Bool unsupported (bool?) Cannot bind to nullable types (bool?, int?, etc) Nov 2, 2018
@akorchev
Copy link

Supporting DateTime? is also needed for <input type="date"> and <input type="datetime-local">

@akorchev
Copy link

@SteveSandersonMS

What would it mean though? How would the UI state ever be “null”?

The UI state is "empty" - the initial state. For example when you bind an <input type="number"> to int? which is currently null the input should stay empty.

I believe both React and Angular behave in a similar way.

@aspnet-hello aspnet-hello added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates area-blazor Includes: Blazor, Razor Components labels Dec 17, 2018
@aspnet-hello aspnet-hello transferred this issue from dotnet/blazor Dec 17, 2018
@s-haralampiev
Copy link

s-haralampiev commented Feb 27, 2019

Hi, what is the state of this issue? Are nullable types supported already in some way? I cannot bind to a nullable type yet (The example above works when binding to a bool? but I am unable to bind to a DateTime?) and I am receiving the same error as stated above.

@julienGrd
Copy link

Hi, same behavior here (to bind a datetime? to an input type=date) , there is a "clean" workaround ?

@mkArtakMSFT
Copy link
Contributor

Thanks for contacting us.
We think that this issue is fixed now. If you are still facing this problem, please file a new issue.

@mkArtakMSFT mkArtakMSFT removed area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates labels May 9, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

10 participants