显示一个字节作为使用EditorTemplate一个复选框? [英] Display a byte as a checkbox using a EditorTemplate?

查看:281
本文介绍了显示一个字节作为使用EditorTemplate一个复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型类:

public class StatusList
{
   public int StatusID {get;set;}
   [UIHint("ByteCheckbox")]
   public byte Active {get;set;}
}

在/查看/共享/ EditorTemplates我创建了一个名为ByteCheckbox.cshtml文件

In /Views/Shared/EditorTemplates I created a file called ByteCheckbox.cshtml

该editortemplate ByteCheckbox包含了(我的第三次尝试):

The editortemplate ByteCheckbox contains (My 3rd attempt):

@model byte
@if (Model == 1)
{
    @Html.CheckBox("", true)
}
else
{
    @Html.CheckBox("", false) 
}

这样做很好地呈现一个复选框。当我更改该复选框状态,并尝试保存模型验证抱怨说,值是'假'的变化(或真),而不是预期的0或1。

Doing this nicely renders a checkbox. When I change the checkbox status and try to save the changes the model validation complains that the value is 'false' (or 'true') instead of the expected 0 or 1.

如何修改editortemplate以允许要翻译的值α

How to modify the editortemplate to allow for the value to be translated?

推荐答案

<击>
您是否尝试过这个?

Have you tried this?

<div class="editor-label">
    @Html.LabelFor(model => model.Active)
</div>
<div class="editor-field">
    @Html.CheckBoxFor(model => model.Active != 0)
    @Html.ValidationMessageFor(model => model.Active)
</div>

您可以在模型中做到这一点:

You can do this in your model:

public class StatusList
{
   public int StatusID {get;set;}
   public byte Active {get;set;}
   [NotMapped]
   public bool ActiveBool
   {
       get { return Active > 0; }
       set { Active = value ? 1 : 0; }
   }
}

这篇关于显示一个字节作为使用EditorTemplate一个复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆