ASP.NET MVC CheckBoxFor的int to bool

当我们使用CheckBoxFor类型需要使用bool ,可以将 int转换成bool

               <div class="form-group">
                    <label class="col-md-3 control-label">选项</label>
                    <div class="col-md-4">
                        @Html.CheckBoxFor(m => m.EnabledValue) 有效
                        @Html.CheckBoxFor(m => m.IsUnfoldValue) 展开
                        @Html.CheckBoxFor(m => m.AllowEditValue) 允许编辑
                        @Html.CheckBoxFor(m => m.AllowDeleteValue) 允许删除
                        <span class="help-block">
                        </span>
                    </div>
                </div>

在实体类添加紫色代码

        /// <summary>
        /// 有效:1-有效,0-无效
        /// </summary>
        public int? Enabled
        {
            set{ _enabled=value;}
            get{return _enabled;}
        }
        public bool EnabledValue
        {
            get { return Enabled == 1; }
            set { Enabled = value ? 1 : 0; }
        }

修改数据

  public ActionResult Edit(string id)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            var model = new SAS.Model.BPMS_SysMenu();
            model=bll.GetModel(id);
            if (model != null)
            {
                ParentDropDownList();  
                return View(model);
            }
            else
            {
                return View("404");
            }
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(string id, FormCollection fc)
        {
            var model = bll.GetModel(id);
            if (model != null)
            {
               
                UpdateModel(model);  //修改数据
                bll.Update(model);
                return RedirectToAction("ModuleList");
            }
            else
                return View("404");
        }

数据库里面

1-有效,0-无效

使用效果比较好

原文地址:https://www.cnblogs.com/cube/p/4884670.html