0

I am working on mvc3. I have created one model which content two property i.e. Name and IsSelect.

Here it is.

public class DemoModel
{
    public string Name { get; set; }
    public bool? IsSelect { get; set; }
}

I am passing this model to view.

    public ActionResult checkbox()
    {
        DemoModel model = getdemoModel();
        return View(model);
    }
    [HttpPost]
    public ActionResult checkbox(DemoModel model)
    {
        ModelState.Clear();
        return View(model);
    }
    public DemoModel getdemoModel()
    {
        DemoModel demoModel = new DemoModel();
        demoModel.Name = "ABC";
        demoModel.IsSelect = null;
        return demoModel;
    } 

Here is my view look like.

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>DemoModel</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.IsSelect)
    </div>
    <div class="editor-field">
        @Html.CheckBoxFor(model => model.IsSelect.Value)
        @Html.ValidationMessageFor(model => model.IsSelect)
    </div>

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
}

When I run this it gives me an error i.e. on @Html.CheckBoxFor(model => model.IsSelect.Value) line.

Nullable object must have a value.

But when I set IsSelect value to false it works fine. But it not returns value for IsSelect property. It returns null value to controller. I think this is very simple issue but I miss something. So, How can I handle null value in @Html.CheckBoxFor? and How can I return it's value to controller?

2 Answers 2

1

First of all , if you dont require the null value , remove ? from the declaration.

Then, pass false value to IsSelect .

bind that to view as follows:

@Html.CheckBoxFor(model=>model.IsSelect)

Updated:

 public static T GetValue<T>(object o)
        {
            T val = default(T);

            if (o != null && o != DBNull.Value)
            {
                val = (T)o;
            }
            return val;
        }

The above snippet is a helper function that checks whether a value is null , if so converts it to its default value.

While reading from the database , use use some thing like : This will help you in casting:

        `bool IsSelect=GetValue<bool>(value)`

Hope this helps..

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

3 Comments

This is just example. I am retrieving values from database which contains null value in IsSelect column
Ok..when you are retireving data from database , inorder to avoid null values , you should type cast those null values into default value of theier type. For example , the default value for bool is false.
How can I cast? That's why I create bool?.
0

A checkbox is not suitable for a null-able Boolean value as it can store 3 states. null,true,false. where as check box can have a true or a false.

If you do want to use checkbox then null state whould be sacrificed to false

    @{
    Model.IsSelect=Model.IsSelect??false;    
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.