0

Trying to figure out checkboxlists to implement in a few areas of our system. We have a table, tblJobs. This has a "AllocatedTo" property, which links to the UserID of "tblGlobalUsers". What we want is to be able to select multiple users, iterate through and insert a database record for each user selected to allocate the job to them.

I've tried following some other questions people asked on here, such as Asp.Net MVC4 Display CheckboxList and some other guides, but just getting nowhere with it unfortunately.

Will provide the code that anyone wants, or more information if needed.

Many thanks

Edit:

Have followed the link Stephen provided and attempted to update my viewmodel and controller as required. My viewmodel is now:

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; }
}

And the controller is:

        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);
    }

Getting an error of "Cannot convert type systems.collections.generic.list to int" on AllocatedTo = model.AllocatedTo,

Community
  • 1
  • 1
South Wilts
  • 121
  • 1
  • 14
  • Refer [this answer](http://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) for one option –  Jul 01 '15 at 09:00
  • Thanks @StephenMuecke taking a look now :) – South Wilts Jul 01 '15 at 09:06
  • @StephenMuecke please see my updated question – South Wilts Jul 01 '15 at 09:12
  • `tblJob.AllocatedTo` is type of `int` but `viewModel.AllocatedTo` to typeof `List` hence the error. Sorry, its unclear what your trying to do. –  Jul 01 '15 at 09:22
  • Well originally it was `AllocatedTo = viewModel.AllocatedTo`, but given that's where the checkbox is being made and after trying to get my around your answer for someone else, that's as far as I was able to get as it made sense given that whatever values are input from the checkboxlist would be going into that column. Make sense? – South Wilts Jul 01 '15 at 09:25
  • _Make sense_? Not at all :) –  Jul 01 '15 at 10:00
  • What is definition of tblJob? – Nikunj Ratanpara Jul 01 '15 at 13:27
  • refer to this for an option - http://stackoverflow.com/a/37221054/2924015 – Nishanth Shaan May 17 '16 at 10:34

0 Answers0