Ajay
Ajay

Reputation: 6590

@html.checkboxfor not return value view to controller

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?

Upvotes: 0

Views: 2781

Answers (2)

Imran
Imran

Reputation: 504

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;    
    }

Upvotes: 0

Sai Avinash
Sai Avinash

Reputation: 4753

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..

Upvotes: 1

Related Questions