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
    Jun 18, 2014 @ 14:42
    Neil Hodges
    0

    Members in v6.16 handling checkbox list data type in members properties

    Hi

    Creating a member in umbraco v6.1.6 i have a checkbox list data type with these values:

    This is the data type for it:

    Im struggling to bind the selected check boxs on the front end to save them against the meber property:

    my code so far is:

    Member createMember = Member.MakeNew(model.FirstName + " " + model.Surname, model.EmailAddress, model.EmailAddress, flaMemberType, adminUser);
    
                //Set password on the newly created member
                createMember.Password = model.Password;
    
                //Set the profile
                createMember.getProperty("title").Value = model.Title;
                createMember.getProperty("firstName").Value = model.FirstName;
                createMember.getProperty("surname").Value = model.Surname;
                createMember.getProperty("firmName").Value = model.FirmName;
                createMember.getProperty("address").Value = model.Address;
                createMember.getProperty("postcode").Value = model.Postcode;
                createMember.getProperty("lng").Value = model.Lng;
                createMember.getProperty("lat").Value = model.Lat;                
                createMember.getProperty("dXLP").Value = model.DXLP;
                createMember.getProperty("tel").Value = model.Tel;
                createMember.getProperty("fax").Value = model.Fax;
                createMember.getProperty("email").Value = model.EmailAddress;
                createMember.getProperty("website").Value = model.Website;
                createMember.getProperty("username").Value = model.Username;
                createMember.getProperty("agreeToTerms").Value = model.AgreeToTerms;
                createMember.getProperty("membershipRenewalDate").Value = DateTime.Now.AddYears(1);
                createMember.getProperty("membershipType").Value = "FLA-Full-Member";
    
                var specs = "";
    
                foreach (var spec in model.AllSpecialism)
                {
                    if (spec.Selected)
                    {
                        specs += spec.Name + ",";
                    } 
                   
                }
                createMember.SetProperty("specialisms", specs);

    its not binding the selected values? i think im close to getting this right just cant see the wood for the tree's been looking at it for so long.

    Can anyone one point out where im going wrong?

     

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Jun 18, 2014 @ 17:46
    Matt Brailsford
    0

    What is model.AllSpecialism? My guess would be that you are storing the "name" in the coma separated string for the "specialisms" property, not the "value".

  • Neil Hodges 338 posts 987 karma points
    Jun 18, 2014 @ 17:58
    Neil Hodges
    0

    Hi Matt

    In my Model View i have setup like this

    And then in the front end of the page for Umbraco begin form:

    when i generate the form in the controller im using this:

     

    Is this the correct way of binding a checkbox list? is there more standard way of doing it? I think yes the 'specialisms' property needs i comma delimited string of id's but that would mean i have to hard code those id's in my controller when creating the checkboxes?

     

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Jun 18, 2014 @ 18:09
    Matt Brailsford
    100

    Hi Neil,

    What you've got seems ok, but yea, I think it comes down to needing the IDs and umbraco expecting the value to be a coma separated list of those IDs, rather than the names.

    Now, you should be able to lookup the prevalues using the relevant Service API (or using the umbraco library helper, but this returns XML so I don't like this method), but you will still end up needing to know something (the ID of the datatype most likely).

    Basically, I think you have 2 options:

    1) Hard code the ID values and hope they don't change 2) Use the Umbraco API to lookup the prevalues, but this means hard coding the DTDID

    You can get creative to lookup the DTDID, but I guess it depends how defensive you want to be.

    Maybe as a test, swap the names in your hard coded list of SpcialismModels to actually be the Umbraco prevalue ids and see if saving those when checked updates in the back office. This way you at least know it is down to the fact you need the ID's and not just the text labels (I'm pretty sure this is the reason though).

    Matt

  • Neil Hodges 338 posts 987 karma points
    Jun 18, 2014 @ 19:11
    Neil Hodges
    0

    Yep

    Tested with passing the values of the datatype checkbox list prevalue id's and hard coded them in the controller

     

    And then on the view just added a hidden filed to pass the value, it works well but dislike the hard coding bit and thought there might be a more elegant way to do it. Not to worry its working now and thats all that matters :)

    I'll look into using the Umbraco API to lookup the prevalues another time as im running out of time on this one but would be nice to see a better solution.

    Thanks for your help Matt.

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Jun 18, 2014 @ 22:08
    Matt Brailsford
    0

    Glad you got it working. If you were using v7 I might have had a better solution for you, but I think those are really your only "simple" options.

  • Paul Wright (suedeapple) 277 posts 704 karma points
    Jun 20, 2014 @ 18:46
    Paul Wright (suedeapple)
    0

    I think you can only get to the prevalue IDs by XML.  Something like this might work.  Then you wont need to hardcode the Names,ID, and you can reorder them in the backoffice with ease

        System.Xml.XPath.XPathNodeIterator pv = umbraco.library.GetPreValues(1134); // The ID of your Property Type
        pv.MoveNext();
        System.Xml.XPath.XPathNodeIterator pvi = pv.Current.SelectChildren("preValue", "");

        while (pvi.MoveNext()) {
                
        new SpecialismModel {Name = pvi.Current.Value, Selected = false, Value = pvi.Current.GetAttribute("id", "")};

        }
  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Jun 20, 2014 @ 20:13
    Matt Brailsford
    0

    Hi Paul,

    Sure it's possible to do it using the umbraco.library helper, but given this is for use in MVC, I can't help think that it's a bit dirty.

    I would probably suggest that we should be using the APIs instead, for example:

    var services = ApplicationContext.Current.Services;
    var prevalues = services.DataTypeService.GetPreValuesCollectionByDataTypeId(1234);
    

    This is much more MVC.

    But as I said in my original post, you still need to know some ID (the ID of the data type (which is true in your case too)), so you are still going to have to hard code something.

    Matt

Please Sign in or register to post replies

Write your reply to:

Draft