我只对此进行了一些有限的测试,但这是您可以使用的扩展方法的基本实现:
public static class HtmlHelperExtensions
{
public static MvcHtmlString CheckBoxesForModel(this HtmlHelper helper,
object model)
{
if (model == null)
throw new ArgumentNullException("'model' is null");
return CheckBoxesForModel(helper, model.GetType());
}
public static MvcHtmlString CheckBoxesForModel(this HtmlHelper helper,
Type modelType)
{
if (modelType == null)
throw new ArgumentNullException("'modelType' is null");
string output = string.Empty;
var properties = modelType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
foreach (var property in properties)
output += helper.CheckBox(property.Name, new { @disabled = "disabled" });
return MvcHtmlString.Create(output);
}
}
您可能希望对其进行扩展以允许它也采用 HTML 属性,而不是对它们进行硬编码,但这应该可以帮助您入门。