选项1
既然您无法向实体添加新属性,为什么不检查操作中的 bool 值呢?
public ActionResult Test(bool isActive)
{
int test = isActive ? 1 : 0;
return View();
}
选项 2
创建您自己的视图模型来检查布尔值,然后创建一个新的实体记录。
模型
public class NikolsModel
{
[Required]
public bool IsActive { get; set; }
// Other Properties...
}
行动
public async Task<ActionResult> Index(NikolsModel model)
{
if (ModelState.IsValid)
{
//
// Create a new instance of your entity
//
var record = new NikolsEntity
{
IsValid = model.IsActive ? 1 : 0,
//Other properties from model
};
DbContext.NikolsEntity.add(record);
await DbContext.SaveChangesAsync();
return RedirectToAction("Index");
}
//Something went wrong, return the view with model errors
return View(model);
}