2

I am using ASP.Net MVC 4 in VB.Net, I have seen a few other answers for how to make a checkbox for a nullable Boolean in c# . Usually converting to VB.Net is pretty straight forward. This I can not figure out.

Other answers say to do this..

make a Boolean.cshtml in my EditorTemplates folder and sticking

@model bool?    
@Html.CheckBox("", Model.GetValueOrDefault())

So, I tried the VB equivelent, in EditorTemplates folder I made Boolean.vbhtml and put this

@model System.Nullable(of Boolean)
@Html.CheckBox("", Model.GetValueOrDefault)

then in my view I have

@Html.EditorFor(Function(x) x.myNullableBool)

That code throws this exception:

Object variable or With block variable not set.

there are a few different places I found with pretty similar c# examples very similar to what is posted here. Does anyone know how to get this to work in VB.Net?

EDIT

This is kind of what I'm trying to do (if it worked it'd be even better)

@model System.Nullable(of Boolean)
@Html.CheckBox("", If(Model is Nothing, False, Model))
2
  • What's the purpose of having Nullable(of Boolean) in your ViewModel but setting it with a checkbox? Still there are three possible 'values'. Better use two different ViewModels instead. And if you currently binding against you DomainModel i would recommend using ViewModels instead. Commented Dec 20, 2012 at 21:41
  • 1
    @milter - i always want one of 2 values true or false , the db has to stay nullable because data can come from other sources , and from those sources they can be null , in my case if it happens to be null - forcing it to false is ok Commented Dec 20, 2012 at 21:45

1 Answer 1

3

An HTML checkbox has 2 states:

  • checked
  • unchecked

Translating into .NET boolean type terms this means:

  • True
  • False

So attempting to bind a Nullable boolean property on your view model to a checkbox hardly makes any logical sense. A nullable boolean has 3 states in .NET:

  • Nothing (null in C#)
  • True
  • False

So as you can see the HTML semantic of a checkbox simply doesn't fit your view model type. It's not surprising by the way why the Html.CheckBoxFor helper expects a boolean property, not nullable boolean.

So I would recommend you modifying the type of your view model property to be a non-nullable boolean if you want to bind it to a checkbox.

Otherwise you could always have a ~/Views/Shared/EditorTemplates/Boolean.vbhtml custom editor template with the following content:

@ModelType Nullable(Of Boolean)
@Html.CheckBox("", If(Model.HasValue, Model.Value, False))

But those kind of hacks should not be necessary if you design your view models correctly i.e. to match the requirements of your views.

Sign up to request clarification or add additional context in comments.

3 Comments

I probably will change view model , but I tried your code and got "Public member 'HasValue' on type 'Boolean' not found."
The ModelType of your custom editor template should be @ModelType Nullable(Of Boolean) (as shown in my answer), not @ModelType Boolean.
ahhh , i had "@model" , I needed "@ModelType" , thanks for the help , I probably will change the ViewModel. but still I feel better now , i've been reading about custom editor templates and now got one to work

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.