试图找出要在我们系统的一些区域实施的复选框列表。我们有一张桌子,tblJobs。这有一个" AllocatedTo" property,链接到" tblGlobalUsers"的UserID。我们想要的是能够选择多个用户,迭代并为所选择的每个用户插入数据库记录以将作业分配给他们。
我已经尝试过按照人们在这里提出的其他一些问题,例如Asp.Net MVC4 Display CheckboxList和其他一些指南,但不幸的是,它无处可去。
将提供任何人想要的代码,或者在需要时提供更多信息。
非常感谢
修改
已按照Stephen提供的链接进行操作,并尝试根据需要更新我的viewmodel和controller。我的viewmodel现在是:
public class JobCreateViewModel
{
public string JobTitle { get; set; }
public string Description { get; set; }
public int InventoryID { get; set; }
public int ReportedBy { get; set; }
public int Priority { get; set; }
public int JobType { get; set; }
public int UserID { get; set; }
public string NetworkLogin { get; set; }
public bool IsSelected { get; set; }
public virtual tblGlobalUser GlobalUserAllocated { get; set; }
public virtual tblGlobalUser GlobalUserReportedBy { get; set; }
public virtual tblInventory InventoryHardware { get; set; }
public virtual tblJobType TypeJob { get; set; }
public virtual tblJobPriority JobsPriority { get; set; }
}
public class JobsAllocateViewModel
{
public JobsAllocateViewModel()
{
AllocatedTo = new List<JobCreateViewModel>();
}
public int UserID { get; set; }
public string NetworkLogin { get; set; }
public List<JobCreateViewModel> AllocatedTo { get; set; }
}
控制器是:
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(JobCreateViewModel viewModel)
{
JobsAllocateViewModel model = new JobsAllocateViewModel();
if(ModelState.IsValid)
{
JobsContext.tblJobs.Add(new tblJob
{
JobTitle = viewModel.JobTitle,
JobDescription = viewModel.Description,
InventoryID = viewModel.InventoryHardware.InventoryID,
ReportedBy = viewModel.GlobalUserReportedBy.UserID,
AllocatedTo = model.AllocatedTo,
Priority = viewModel.JobsPriority.PriorityID,
JobType = viewModel.TypeJob.TypeID
});
JobsContext.SaveChanges();
return RedirectToAction("Index");
}
return View(viewModel);
}
获取错误&#34;无法将类型systems.collections.generic.list转换为int&#34;在AllocatedTo = model.AllocatedTo,