如何在MVC4中从View中将Checkbox检查或不检查传递给Controller

时间:2016-02-28 09:56:34

标签: asp.net-mvc-4

我正在使用带有剃刀视图的MVC4,我想将Checkboxfor的Checked或Not值从Create View传递给Controller的create方法但在post方法中我在 m_category.IsActive 中得到null值任何人都可以帮我解决我的问题,我的创建视图按照下面给出的

@model Test.Models.M_Category
....
@using (Html.BeginForm("Create", "Category"))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    ....
    <div class="form-group">
        @Html.LabelFor(model => model.CategoryName, "Category Name")
        @Html.TextBoxFor(model => model.CategoryName, new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.CategoryName)
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.IsActive, "Is Active")
        @Html.CheckBoxFor(model => model.IsActive.Value, new { @class = "checkbox" })                                    
        @Html.ValidationMessageFor(model => model.IsActive)
    </div>
    ....
}

和我的创建控制器方法

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(M_Category m_category)
    {
        if (ModelState.IsValid)
        {

             M_Category obj = new M_Category();
            obj = db.M_Category.Where(x => x.CategoryName.ToLower() == m_category.CategoryName && x.CategoryID!=m_category.CategoryID && x.IsDeleted == false).FirstOrDefault();
            if (obj == null)
            {
                obj = new M_Category();
                obj = db.M_Category.Find(m_category.CategoryID);
                obj.IsActive = m_category.IsActive;//here I am getting null value
                obj.CategoryName = m_category.CategoryName;
                db.Entry(obj).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
        }
        TempData["exists"] = "record already exists.";
        return RedirectToAction("Index");
    }

提前致谢..

0 个答案:

没有答案