C# – Make Html.CheckBoxFor return an integer

asp.netasp.net-mvcccheckboxhtml.checkbox

I want to bind a checkbox to an integer (the ISACTIVE value shown below) in ASP.net MVC.

@Html.CheckBoxFor(model => model.ISACTIVE, new { htmlAttributes = new { @class = "form-control" } })

I know that Html.CheckBoxFor only accepts bool as input and I could add a new property on my Model, but I'm using an already existing database and every time it updates, the Model gets refreshed.

Is there a way to create a new method for CheckBoxFor that would return an integer based on if the box is checked or not?

Best Answer

You could also try using simple HTML input control for checkbox type. That way, you can assign some value or name to it and return it to the controller as well.

This might not be the exact thing that you are trying to achieve. It would give you an idea though.

In your view :

<input type="checkbox" id="yourId" name="selectedIds" value="@menu.Id"/>

In your controller, you can try accessing this particular control's value like this :

value = Request.Form["selectedIds"];

Hope this helps.

Related Topic