I need help to build a CheckBoxFor for getting an int
value.
Something like:
@Html.CheckBoxForInt(m => m.foo.intValue)
It should be checked if intValue = 1
else not checked.
Why don't you expose a bool property in your model that converts to/from the int?
Something like this:
public bool BoolValue
{
get { return IntValue == 1; }
set { IntValue = value ? 1 : 0;}
}
public int IntValue { get; set; }
Then you could use it to create the checkbox
@Html.CheckBoxFor(m => m.foo.BoolValue)
For some reason the response above gave me errors but based on the same idea I've change the code like this:
public int IntValue { get; set; }
public bool BoolValue
{
get { return IntValue == 1; }
set {
if(value)
IntValue = 1;
else
IntValue = 0;
}
}
and that work for me.
Here's the checkbox helper example for handling int values:
public static MvcHtmlString CheckBoxIntFor<TModel>(this HtmlHelper<TModel> html, Expression<Func<TModel, int>> expression, object htmlAttributes)
{
// get the name of the property
string[] propertyNameParts = expression.Body.ToString().Split('.');
// create name and id for the control
string controlId = String.Join("_", propertyNameParts.Skip(1));
string controlName = String.Join(".", propertyNameParts.Skip(1));
// get the value of the property
Func<TModel, int> compiled = expression.Compile();
int booleanSbyte = compiled(html.ViewData.Model);
// convert it to a boolean
bool isChecked = booleanSbyte == 1;
// build input element
TagBuilder checkbox = new TagBuilder("input");
checkbox.MergeAttribute("id", controlId);
checkbox.MergeAttribute("name", controlName);
checkbox.MergeAttribute("type", "checkbox");
if (isChecked)
{
checkbox.MergeAttribute("checked", "checked");
checkbox.MergeAttribute("value", "1");
}
else
{
checkbox.MergeAttribute("value", "0");
}
SetStyle(checkbox, htmlAttributes);
// script to handle changing selection
string script = "<script>" +
"$('#" + controlId + "').change(function () { " +
"if ($('#" + controlId + "').is(':checked')) "+
"$('#" + controlId + "').val('1'); " +
"else " +
"$('#" + controlId + "').val('0'); " +
"}); " +
"</script>";
return MvcHtmlString.Create(checkbox.ToString(TagRenderMode.SelfClosing) + script);
}
private static void SetStyle(TagBuilder control, object htmlAttributes)
{
if(htmlAttributes == null)
return;
// get htmlAttributes
Type t = htmlAttributes.GetType();
PropertyInfo classInfo = t.GetProperty("class");
PropertyInfo styleInfo = t.GetProperty("style");
string cssClasses = classInfo?.GetValue(htmlAttributes)?.ToString();
string style = styleInfo?.GetValue(htmlAttributes)?.ToString();
if (!string.IsNullOrEmpty(style))
control.MergeAttribute("style", style);
if (!string.IsNullOrEmpty(cssClasses))
control.AddCssClass(cssClasses);
}
A slightly different approach:
my database needs to accept an int, and i want to have a checkbox that sends that int from my form in Razor. on the page i also use angularJS (also easy to just use pure JS). So here is a solution that does not involve changing models etc.
in form:
<input ng-click="checkCheckBox()" type="checkbox" id="myCheck">
@Html.HiddenFor(m => m.Role, new { id = "id_checkbox", Value = 0 })
in javascript:
$scope.checkCheckBox= function(){
var x = document.getElementById("myCheck").checked;
if (x == true) { $('#id_checkbox').val(3);}
else { $('#id_checkbox').val(0);}
}