Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Neil Hodges 338 posts 987 karma points
    May 28, 2014 @ 16:07
    Neil Hodges
    0

    Best way to deal with mulitple checkboxes in BeginUmbracoForm

    Hi

    Using MVC Umbraco 6.1.6

    Still finding my feet with MVC and wanted to ask the best way to deal with multiple checkboes in a BeginUmbracoForm. What i have so far:

    The View

    @model RegisterViewModel
     using (Html.BeginUmbracoForm<AuthSurfaceController>("HandleRegister"))     {         @Html.AntiForgeryToken()         @Html.ValidationSummary(false)         <fieldset>                          <div class="form-group">                 @Html.LabelFor(model => model.Title, new { @class = ""})                 @Html.DropDownListFor(model => model.Title, new SelectList(new List<Object>{ new { value = 0 , text = "Please Select..."  }, new { value = 1 , text = "Mr"  }, new { value = 2 , text = "Mrs" }, new { value = 3 , text = "Miss"} }, "value""text", Model.Title), new { @class = "title-select"})                 @Html.ValidationMessageFor(model => model.Title, string.Empty, new { @class= "" })             </div>             <div class="form-group">                 @Html.LabelFor(model => model.FirstName, new { @class = ""})                 @Html.TextBoxFor(model => model.FirstName, new{ @class = "" })                 @Html.ValidationMessageFor(model => model.FirstName, string.Empty, new { @class= "" })             </div>                          <div class="form-group">                 @Html.LabelFor(model => model.Surname, new { @class = ""})                 @Html.TextBoxFor(model => model.Surname, new{ @class = "" })                 @Html.ValidationMessageFor(model => model.Surname, string.Empty, new { @class= "" })             </div>                          <div class="form-group">                 @Html.LabelFor(model => model.FirmName, new { @class = ""})                 @Html.TextBoxFor(model => model.FirmName, new{ @class = "" })                 @Html.ValidationMessageFor(model => model.FirmName, string.Empty, new { @class= "" })             </div>                          <div class="form-group">                 @Html.LabelFor(model => model.Address, new { @class = ""})                 @Html.TextBoxFor(model => model.Address, new{ @class = "" })                 @Html.ValidationMessageFor(model => model.Address, string.Empty, new { @class= "" })             </div>                          <div class="form-group">                 @Html.LabelFor(model => model.Postcode, new { @class = ""})                 @Html.TextBoxFor(model => model.Postcode, new{ @class = "" })                 @Html.ValidationMessageFor(model => model.Postcode, string.Empty, new { @class= "" })             </div>                          <div class="form-group">                 @Html.LabelFor(model => model.DXLP, new { @class = ""})                 @Html.TextBoxFor(model => model.DXLP, new{ @class = "" })                 @Html.ValidationMessageFor(model => model.DXLP, string.Empty, new { @class= "" })             </div>                          <div class="form-group">                 @Html.LabelFor(model => model.Tel, new { @class = ""})                 @Html.TextBoxFor(model => model.Tel, new{ @class = "" })                 @Html.ValidationMessageFor(model => model.Tel, string.Empty, new { @class= "" })             </div>                          <div class="form-group">                 @Html.LabelFor(model => model.Fax, new { @class = ""})                 @Html.TextBoxFor(model => model.Fax, new{ @class = "" })                 @Html.ValidationMessageFor(model => model.Tel, string.Empty, new { @class= "" })             </div>                                     <div class="form-group">                 @Html.LabelFor(model => model.EmailAddress, new { @class = ""})                 @Html.TextBoxFor(model => model.EmailAddress, new{ @class = "" })                 @Html.ValidationMessageFor(model => model.EmailAddress, string.Empty, new { @class= "" })             </div>                          <div class="form-group">                 @Html.LabelFor(model => model.Website, new { @class = ""})                 @Html.TextBoxFor(model => model.Website, new{ @class = "" })                 @Html.ValidationMessageFor(model => model.Tel, string.Empty, new { @class= "" })             </div>                          @* specialisms need to list specialisms as check boxes here*@                                       <div class="form-group">                 @Html.LabelFor(model => model.Username, new { @class = ""})                 @Html.TextBoxFor(model => model.Username, new{ @class = "" })                 @Html.ValidationMessageFor(model => model.Username, string.Empty, new { @class= "" })             </div>                          <div class="form-group">                 @Html.LabelFor(model => model.Password, new { @class = ""})                 @Html.PasswordFor(model => model.Password, new{ @class = ""})                 @Html.ValidationMessageFor(model => model.Password , string.Empty, new { @class= "" })             </div>                      <div class="form-group">                 @Html.LabelFor(model => model.ConfirmPassword, new { @class = ""})                 @Html.PasswordFor(model => model.ConfirmPassword, new{ @class = "" })                 @Html.ValidationMessageFor(model => model.ConfirmPassword, string.Empty, new { @class= "" })             </div>                          <div class="form-group">                 @Html.LabelFor(model => model.Username, new { @class = ""})                 @Html.CheckBoxFor(model => model.AgreeToTerms, new{ @class = "" })                 @Html.ValidationMessageFor(model => model.Username, string.Empty , new { @class= "" })             </div>                          <button type="submit" class="btn btn-default">Register</button>                  </fieldset>     }

     

    Model:

    public class RegisterViewModel
        {
            [Required(ErrorMessage = "Please select a title")]
            public string Title { getset; }
    
            [Required(ErrorMessage = "Please enter first name")]
            public string FirstName { getset; }
    
            [Required(ErrorMessage = "Please enter surname")]
            public string Surname { getset; }
    
            [Required(ErrorMessage = "Please enter firm name")]
            public string FirmName { getset; }
    
            [Required(ErrorMessage = "Please enter address")]
            public string Address { getset; }
    
            [Required(ErrorMessage = "Please enter postcode")]
            public string Postcode { getset; }
    
            [Required(ErrorMessage = "Please enter DX/LP")]
            public string DXLP { getset; }
    
            [Required(ErrorMessage = "Please enter telephone number")]
            public string Tel { getset; }
            
            public string Fax { getset; }
    
            [DisplayName("Email address")]
            [Required(ErrorMessage = "Please enter your email address")]
            [EmailAddress(ErrorMessage = "Please enter a valid email address")]
            [Remote("CheckEmailIsUsed""AuthSurface", ErrorMessage = "The email address has already been registered")]
            public string EmailAddress { getset; }
    
            [Required(ErrorMessage = "Please enter website address")]
            public string Website { getset; }
    
            public IEnumerable<SpecialismModel> AllSpecialism { getset; }
    
            [Required]
            [Remote("CheckProfileUserNameAvailable""ProfileSurface", ErrorMessage = "The User Name is already in use")]
            [DisplayName("Desired Username")]
            public string Username { getset; }
            
            [DataType(DataType.Password)]
            [Required(ErrorMessage = "Please enter your password")]
            public string Password { getset; }
    
            [DataType(DataType.Password)]
            [DisplayName("Confirm Password")]
            [Required(ErrorMessage = "Please enter your password")]
            [System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "Your passwords do not match")]
            public string ConfirmPassword { getset; }
    
            public bool AgreeToTerms { getset; }
    
        }
    
        public class SpecialismModel
        {
            public string Name { getset; }
            public bool Selected { getset; }
        }

     

    Think im not too far away from it, just confused where i can add the list of Specialisms, does this need to be added in the Model?

    Any help greatly appreciated.

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

  • Neil Hodges 338 posts 987 karma points
    May 28, 2014 @ 17:06
    Neil Hodges
    0

    Ok think ive sussed this myself, here is what i did just in case anyone else needs to do the same

    in my Model i added this:

    public List<SpecialismModel> AllSpecialism { getset; }
    
     public class SpecialismModel
        {
            public string Name { getset; }
            public bool Selected { getset; }
        }

     

    And then in my Surface Controller i added the following:

     public ActionResult RenderRegister()
            {
                var model = new RegisterViewModel
                {
                    AllSpecialism = new List<SpecialismModel>
                    {
                        new SpecialismModel {Name = "Family Law Accreditation"},
                        new SpecialismModel {Name = "Child Law Accreditation"},
                        new SpecialismModel {Name = "Notary Public"},
                        new SpecialismModel {Name = "Solicitor Advocate"},
                        new SpecialismModel {Name = "Collaborative Lawyer"},
                        new SpecialismModel {Name = "Calm Mediator"},
                        new SpecialismModel {Name = "Legal Aid Offered"},
                        new SpecialismModel {Name = "Advocate"},
                        new SpecialismModel {Name = "Family Law Arbitrator"}
                    }
                };
    
                return PartialView("Register", model);
            }

     

    And finally in my View i could do this in the BeginUmbracoForm:

     @* specialisms need to list specialisms as check boxes here*@
                <div class="form-group">
                    @for (int i = 0; i < Model.AllSpecialism.Count; i++)
                    {
                        @Html.CheckBoxFor(m => m.AllSpecialism[i].Selected, new { id = "specialismType_" + i })
                        @Html.HiddenFor(m => m.AllSpecialism[i].Name)
                        @Html.DisplayFor(m => m.AllSpecialism[i].Name)
                    }
                    @Html.ValidationMessageFor(model => model.AllSpecialism, string.Empty, new { @class= "" })
                </div>

     

    Hopefully this will work Ok :)

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

  • simon kerr 31 posts 85 karma points
    Jul 15, 2014 @ 16:49
    simon kerr
    0

    Excellent, thanks a lot. That really helped! For me, I needed to persist names and values across so I used...

    @Html.CheckBoxFor(m => m.ExternalAtoZProviders[i].Selected, new { @id = Model.ExternalAtoZProviders[i].Value })
    @Html.HiddenFor(m => m.ExternalAtoZProviders[i].Value)
    @Html.HiddenFor(m => m.ExternalAtoZProviders[i].Title)
    @Html.Label(Model.ExternalAtoZProviders[i].Value, Model.ExternalAtoZProviders[i].Title)
    

    This seems to be working, but if a better way is there, I'd be very interested to hear it.

  • Neil Hodges 338 posts 987 karma points
    Jul 15, 2014 @ 19:01
    Neil Hodges
    0

    Hi Simon

    Glad it helped,

    yeh i have tweaked it a little, and pretty much have the same as you have now as i needed the Pre Value to assign the correct option in the Checkbox list DataType.

    I did however have another thread where some other options were presented but not had time to go over them properly, here is the thread, may help you some more - http://our.umbraco.org/forum/developers/api-questions/53769-Members-in-v616-handling-checkbox-list-data-type-in-members-properties

    Cheers

    Neil.

Please Sign in or register to post replies

Write your reply to:

Draft