【发布时间】:2012-11-22 15:45:00
【问题描述】:
我需要帮助来构建 CheckBoxFor 以获取 int 值。
类似:
@Html.CheckBoxForInt(m => m.foo.intValue)
如果intValue = 1 则应选中,否则未选中。
【问题讨论】:
标签: asp.net-mvc-3 razor custom-controls
我需要帮助来构建 CheckBoxFor 以获取 int 值。
类似:
@Html.CheckBoxForInt(m => m.foo.intValue)
如果intValue = 1 则应选中,否则未选中。
【问题讨论】:
标签: asp.net-mvc-3 razor custom-controls
为什么不在你的模型中公开一个 bool 属性来转换为 int 或从 int 转换?
类似这样的:
public bool BoolValue
{
get { return IntValue == 1; }
set { IntValue = value ? 1 : 0;}
}
public int IntValue { get; set; }
然后你可以用它来创建复选框
@Html.CheckBoxFor(m => m.foo.BoolValue)
【讨论】:
由于某种原因,上面的响应给了我错误,但基于相同的想法,我已经更改了这样的代码:
public int IntValue { get; set; }
public bool BoolValue
{
get { return IntValue == 1; }
set {
if(value)
IntValue = 1;
else
IntValue = 0;
}
}
这对我有用。
【讨论】:
这是处理 int 值的复选框帮助器示例:
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);
}
【讨论】:
稍微不同的方法:
我的数据库需要接受一个 int,并且我希望有一个复选框,可以从我在 Razor 中的表单发送该 int。在页面上我也使用了 angularJS(也很容易使用纯 JS)。所以这是一个不涉及更改模型等的解决方案。
形式:
<input ng-click="checkCheckBox()" type="checkbox" id="myCheck">
@Html.HiddenFor(m => m.Role, new { id = "id_checkbox", Value = 0 })
在javascript中:
$scope.checkCheckBox= function(){
var x = document.getElementById("myCheck").checked;
if (x == true) { $('#id_checkbox').val(3);}
else { $('#id_checkbox').val(0);}
}
【讨论】: