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、2ActiveNme:取值为"yes"、"no"
内容的提问来源于stack exchange,提问作者Fatimma-0
相关产品推荐
相关产品推荐

