1

I'm new to MVC and building a page that has some form fields on and then a list of data with a checkbox on each item in the list.

The user completes the form and checks the required checkboxes and then submits the form. The issue is that the list of checkboxes always returns null.

View Model - information on the client and user and then a list to display with checkboxes.

public class ClientUserViewModel
{
    public Client ClientDetail { get; set; }
    public ClientUser ClientUserDetail { get; set; }
    public List<ClientSite> ClientSitesList { get; set; }
}

Client Site model - data for the site list and SelectedSites to hold the checkbox state

public class ClientSite
{
        public int SiteId { get; set; }
        public int ClientId { get; set; }
        public string SiteName { get; set; }
        public string Address1 { get; set; }
        public string Address2 { get; set; }
        public string Town { get; set; }
        public string County { get; set; }
        public string Postcode { get; set; }
        public string Tel { get; set; }
        public string Email { get; set; }
        public bool Enabled { get; set; }
        public bool SelectedSites { get; set; }
}

List Part of the View

@foreach (var item in Model.ClientSitesList)
        {
                <tr>
                    <td>@item.SiteName</td>
                    <td>
                        @{
                            string fullAddress = "";

                            fullAddress += (String.IsNullOrEmpty(@item.Address1) ? "" : @item.Address1);
                            fullAddress += (String.IsNullOrEmpty(@item.Address2) ? "" : ", " + @item.Address2);
                            fullAddress += (String.IsNullOrEmpty(@item.Town) ? "" : ", " + @item.Town);
                            fullAddress += (String.IsNullOrEmpty(@item.Postcode) ? "" : ", " + @item.Postcode);
                        }
                        @fullAddress
                    </td>
                    <td>@item.Tel</td>
                    <td><a href="mailto:@(item.Email)">@item.Email</a></td>
                    <td style="text-align:right;">
                        @Html.CheckBoxFor(model => item.SelectedSites, new { @class = "form-control chk"})
                    </td>
                </tr>
        }

Controller

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ApplicationDbContext DbContext, ClientUserViewModel model, List<ClientSite> ClientSitesList)
    {

               // create new user under selected client
                try
                {
                    if (ModelState.IsValid)
                    {
                        IdentityResult ir;
                        var rm = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(DbContext));
                        //ir = rm.Create(new IdentityRole("pxpuser"));

                        var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(DbContext));
                        um.UserValidator = new UserValidator<ApplicationUser>(UserManager)
                        {
                            AllowOnlyAlphanumericUserNames = false
                        };

                        var user = new ApplicationUser()
                        {
                            UserName = model.ClientUserDetail.Email,
                            Email = model.ClientUserDetail.Email,
                            FirstName = model.ClientUserDetail.Firstname,
                            LastName = model.ClientUserDetail.Surname,
                            UserGroup = "Client"
                        };

                        ir = um.Create(user, model.ClientUserDetail.Password);

                        if (ir.Succeeded == false)
                        {
                            Response.Write(ir.Errors);
                            return View(model);
                        }
                        else
                        {
                            // add new user to pxpclient role
                            ir = um.AddToRole(user.Id, "pxpclient");



                            // insert sites user has access to
                            foreach (var site in model.ClientSitesList)
                            {
                                if (site.SelectedSites == true)
                                {
                                    var userSites = new PgsUsersSite()
                                    {
                                        UserId = user.Id,
                                        SiteId = site.SiteId
                                    };
                                    context.PgsUsersSites.InsertOnSubmit(userSites);
                                    context.SubmitChanges();
                                }
                            }


                            // insert new client user details
                            var clientUser = new PgsUser()
                            {
                                UserId = user.Id,
                                ClientId = model.ClientDetail.ClientId,
                                Email = model.ClientUserDetail.Email,
                                Firstname = model.ClientUserDetail.Firstname,
                                Surname = model.ClientUserDetail.Surname,
                                AppointmentBooked = model.ClientUserDetail.AppointmentBooked,
                                PostVisit = model.ClientUserDetail.PostVisit,
                                PxOffer = model.ClientUserDetail.PxOffer,
                                SurveyInstruction = model.ClientUserDetail.SurveyInstruction,
                                EstateAgentInstruction = model.ClientUserDetail.EstateAgentInstruction,
                                BuyinExchange = model.ClientUserDetail.BuyinExchange,
                                BuyinNotes = model.ClientUserDetail.BuyinNotes,
                                SolicitorInstruction = model.ClientUserDetail.SolicitorInstruction,
                                AccessCaseDetails = model.ClientUserDetail.AccessCaseDetails,
                                AccessFeedback = model.ClientUserDetail.AccessFeedback,
                                AccessImages = model.ClientUserDetail.AccessImages,
                                AccessValuations = model.ClientUserDetail.AccessValuations,
                                AccessPxOffer = model.ClientUserDetail.AccessPxOffer,
                                AccessLegal = model.ClientUserDetail.AccessLegal,
                                AccessReports = model.ClientUserDetail.AccessReports
                            };
                            context.PgsUsers.InsertOnSubmit(clientUser);
                            context.SubmitChanges();





                        }

                        return RedirectToAction("Edit", "Clients", new { id = model.ClientUserDetail.ClientId });

                    }
                }
            catch (Exception ex)
            {

                ModelState.AddModelError(string.Empty, ex.Message);
            }



            return View(model);
}

So model.ClientSitesListalways returns nullno matter how many checkboxes are selected.

Jammer
  • 2,330
  • 11
  • 48
  • 77

0 Answers0