我有一个模型接受代码为NULL,空和任何其他字符串值以及IsCodeNull
bool
属性
public class Translation : IEquatable<Translation>
{
private string code;
private bool isCodeNull;
[MaxLength(30)]
public string Code
{
get
{
if (code == null)
isCodeNull = true;
else
isCodeNull = false;
return code;
}
set
{
code = value;
}
}
[NotMapped]
public bool IsCodeNull
{
get { return isCodeNull; }
set { isCodeNull = value; }
}
}
--- ----查看
<div class="form-group">
@Html.LabelFor(model => model.Code, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Code)
@Html.CheckBoxFor(model => model.IsCodeNull) (check this box, If value is NULL)
@Html.ValidationMessageFor(model => model.Code)
</div>
当复选框为true时,如何在数据库中存储空值,如果为false则存储string.empty。
我无法使用dbNull.Value
:它给我一个错误,它无法明确地从null
转换为string
。