You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MVC中通过单选按钮实现表格行显示/隐藏问题求助

问题:用单选按钮控制员工列表行的显示与隐藏

需求

我从数据库获取了员工列表,希望添加单选按钮来控制列表中表格行的显示与隐藏,但尝试编写代码后未能实现需求,特此寻求帮助。

现有实现代码

View中的单选按钮部分

@*  ---Active part*@ 
<div class="form-group">
    <div class="row">
        <div class="col-md-3">
            <label>Correct Identification</label>
        </div>
        <div class="col=md-4">
            <div class="form-check form-check-inline">
                @Html.RadioButton("IsCorrectIdentification", 1, new { @class = "form-check-input" })<span>Yes</span>
                @Html.RadioButton("IsCorrectIdentification", 0, new { @class = "form-check-input" })<span>No</span>
            </div>
        </div>
        <div class="col-md-5">
            @Html.ValidationMessageFor(model => model.Active, "", new { @class = "text-danger" })
        </div>
    </div>
</div>

Controller代码

Active Action方法

public ActionResult Active() 
{
    return View();
}

[HttpPost]
public ActionResult Active(Employee Model)
{
    if (ModelState.IsValid)
    {
        var Employee = db.Employees.Where(x => x.EmID == Model.EmID).SingleOrDefault();
        if (Employee != null)
        {
            // 此处逻辑不完整
        }
    }
    // 缺少返回逻辑
}

编辑页EditEm2 Action方法

public ActionResult EditEm2(int? id)
{
    var Result = db.Employees.Where(x => x.EmID == id).SingleOrDefault();
    ViewBag.DepartmentData = db.Departments.ToList();
    ViewBag.GenderData = db.Genders.ToList();
    ViewBag.NationalityData = db.Nationalities.ToList();
    return View(Result);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditEm2(Employee Model)
{
    if (ModelState.IsValid)
    {
        var Employee = db.Employees.Where(x => x.EmID == Model.EmID).SingleOrDefault();
        if (Employee != null)
        {
            Employee.EmID = Model.EmID;
            Employee.EmNameEN = Model.EmNameEN;
            Employee.EmNameAR = Model.EmNameAR;
            Employee.NationalityID = Model.NationalityID;
            Employee.BirthDate = Model.BirthDate;
            Employee.EmplymentDate = Model.EmplymentDate;
            Employee.GenderID = Model.GenderID;
            Employee.DepartmentID = Model.DepartmentID;
            Employee.ContactPhone = Model.ContactPhone;
            db.Entry(Employee).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();
        }

        return RedirectToAction("indexEm");
    }
    else
    {
        EditEm2(Model.EmID);
    }
    return View();
}

数据库表结构

存在一张Active表,字段如下:

  • ActiveId:取值为1、2
  • ActiveNme:取值为"yes"、"no"

内容的提问来源于stack exchange,提问作者Fatimma-0

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 00:00:59