ASP.. NET MVC编辑数据库中的CheckBoxList值
本文关键字:CheckBoxList 数据库 NET MVC 编辑 ASP | 更新日期: 2023-09-27 18:15:52
我很难理解如何从数据库中的CustomerDevice表检索和编辑DevId值到基于CustId值的CheckBoxList。
CustomerDeviceController的My Index Action Method显示客户表中的客户列表。我有一个ActionLink标签"编辑"传递CustId值CustomerDeviceController [HttpGet]编辑(int?id)当前显示设备表中所有checkboxlisttitem值的动作方法。然而,CheckBoxList并没有将数据库中的CustomerDevice表中选中的DevId值显示到属于CustId的CheckBoxList中,而是显示对每个CheckBoxList值的检查。
部分,我有困难的理解和弄清楚,是我如何显示所选的DevId值从CustomerDevice表在我的数据库基于CustId的CheckBoxList,然后编辑/更新修改的CheckBoxListItems在[HttpPost]编辑动作方法回到我的CustomerDevice表在我的数据库中,如果需要的话。
请参阅下面的代码,我目前为止。
public class CheckBoxListItem
{
public int ID { get; set; }
public string Display { get; set; }
public bool IsChecked { get; set; }
}
public class Customer
{
public int CustId { get; set; }
public string CustDisplayName { get; set; }
public string CustFirstName { get; set; }
....
}
public class Device
{
public int DevId { get; set; }
public string DevType { get; set; }
}
public class CustomerDevice
{
public int CustId { get; set; }
public int DevId { get; set; }
public Customer Customer { get; set; }
public Device Device { get; set; }
}
视图模型
public class CustomerDeviceFormViewModel
{
public int CustId { get; set; }
public string CustDisplayName { get; set; }
public List<CheckBoxListItem> Devices { get; set; }
}
CustomerDeviceController
public ActionResult Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var customervm = new CustomerDeviceFormViewModel();
Customer customer = db.Customers.SingleOrDefault(c => c.CustId == id);
if (customer == null)
{
return NotFound();
}
customervm.CustId = customer.CustId;
customervm.CustDisplayName = customer.CustDisplayName;
// Retrieves list of Devices for CheckBoxList
var deviceList = db.Devices.ToList();
var checkBoxListItems = new List<CheckBoxListItem>();
foreach (var device in deviceList)
{
checkBoxListItems.Add(new CheckBoxListItem()
{
ID = device.DevId,
Display = device.DevType,
IsChecked = deviceList.Where(x => x.DevId == device.DevId).Any()
});
}
customervm.Devices = checkBoxListItems;
return View(customervm);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(CustomerDeviceFormViewModel vmEdit)
{
if (ModelState.IsValid)
{
Customer customer = db.Customers.SingleOrDefault(c => c.CustId == vmEdit.CustId);
if (customer == null)
{
return NotFound();
}
foreach (var deviceId in vmEdit.Devices.Where(x => x.IsChecked).Select(x => x.ID))
{
var customerDevices = new CustomerDevice
{
CustId = vmEdit.CustId,
DevId = deviceId
};
db.Entry(customerDevices).State = EntityState.Modified;
}
db.SaveChanges();
return RedirectToAction("Index");
}
return View(vmEdit);
}
Edit.chtml
<div class="form-group">
Please select the Devices to assign to <b>@Html.DisplayFor(c => c.CustDisplayName)</b>
</div>
<div class="form-group">
@Html.EditorFor(x => x.Devices)
</div>
@Html.HiddenFor(c => c.CustId)
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
共享/EditorTemplate CheckBoxListItem.chtml
<div class="checkbox">
<label>
@Html.HiddenFor(x => x.ID)
@Html.CheckBoxFor(x => x.IsChecked)
@Html.LabelFor(x => x.IsChecked, Model.Display)
</label>
<br />
您设置IsChecked
值的代码将始终返回true
(您的循环基本上是说,如果集合包含我(当然它这样做),然后将其设置为true)。
您需要通过读取CustomerDevice
表中的值来获得每个Customer
的选定值
Customer customer = db.Customers.SingleOrDefault(c => c.CustId == id);
if (customer == null)
{
return NotFound();
}
// Get all devices
var deviceList = db.Devices.ToList();
// Get the selected device ID's for the customer
IEnumerable<int> selectedDevices = db.CustomerDevices
.Where(x => x.CustId == id).Select(x => x.DevId);
// Build view model
var model = new CustomerDeviceFormViewModel()
{
CustId = customer.CustId,
CustDisplayName = customer.CustDisplayName,
Devices = deviceList.Select(x => new CheckBoxListItem()
{
ID = x.DevId,
Display = x.DevType,
IsChecked = selectedDevices.Contains(x.DevId)
}).ToList()
};
return View(model);
下面是我使用过的Razor代码片段:
foreach (SelectListItem p in Model.PositionList)
{
@Html.Raw(p.Text + "<input type=checkbox name='"PositionIDs'" id='"PositionIDs'" value=" + @p.Value + (Model.Positions != null && Model.Positions.Any(pos => pos.ScoreCardId == Convert.ToInt32(p.Value)) ? " checked />" : " />"));
}
你可能想看看MvcCheckBoxList NuGet包:
https://www.nuget.org/packages/MvcCheckBoxList/这使得在MVC中使用CheckBoxList做一些强大的事情变得更加容易-并且可能是修复CheckBox问题的更好方法。