我已经这样做了。为 CheckBoxFor 编写您自己的 ExtensionMethod。诀窍是复选框中“value”=“true”的静态值和隐藏字段中的“value”“false”。如前所述,不会发回值为 false 的复选框。在这种情况下,将采用隐藏字段的值。当用户选中该复选框时,新的“true”值将覆盖隐藏字段中的“false”。
public static MvcHtmlString CheckboxForMetro<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, int offset = 3)
{
TagBuilder tblabel = new TagBuilder("label");
tblabel.AddCssClass("checkbox offset" + offset.ToString());
TagBuilder tbinput = new TagBuilder("input");
tbinput.Attributes.Add("type", "checkbox");
tbinput.Attributes.Add("id", GetPropertyNameFromLambdaExpression(html, expression));
tbinput.Attributes.Add("name", GetPropertyNameFromLambdaExpression(html, expression));
tbinput.Attributes.Add("value", "true");
tbinput.MergeAttributes(GetPropertyValidationAttributes(html, expression, null));
if (GetPropertyValueFromLambdaExpression(html, expression) == "True") tbinput.Attributes.Add("checked", "checked");
TagBuilder tbhidden = new TagBuilder("input");
tbhidden.Attributes.Add("type", "hidden");
tbhidden.Attributes.Add("value", "false");
tbhidden.Attributes.Add("name", GetPropertyNameFromLambdaExpression(html, expression));
TagBuilder tbspan = new TagBuilder("span");
//tbspan.AddCssClass("span" + spanLabel.ToString());
tbspan.InnerHtml = GetPropertyDisplayNameFromLambdaExpression(html, expression);
tblabel.InnerHtml = tbinput.ToString() + tbspan.ToString() + tbhidden.ToString();
return new MvcHtmlString(tblabel.ToString());
}
它是 Metro UI CSS 的 ExtensionMethod,位于http://metroui.org.ua
这是获取值、显示名称、属性名称和验证属性的代码
private static string GetPropertyDisplayNameFromLambdaExpression<TModel, TValue>(HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
return metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last() ?? "Geen tekst";
}
private static string GetPropertyValueFromLambdaExpression<TModel, TValue>(HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
string value = string.Empty;
TModel model = html.ViewData.Model;
if (model != null)
{
var expr = expression.Compile().Invoke(model);
if (expr != null)
{
value = expr.ToString();
}
}
return value;
}
private static string GetPropertyNameFromLambdaExpression<TModel, TValue>(HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
return metadata.PropertyName;
}
private static IDictionary<string, object> GetPropertyValidationAttributes<TModel, TValue>(HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
IDictionary<string, object> validationAttributes = html.GetUnobtrusiveValidationAttributes(ExpressionHelper.GetExpressionText(expression), metadata);
if (htmlAttributes == null)
{
htmlAttributes = validationAttributes;
}
else
{
htmlAttributes = htmlAttributes.Concat(validationAttributes).ToDictionary(k => k.Key, v => v.Value);
}
return htmlAttributes;
}
我希望这对其他人有帮助。