Copied to clipboard

Flag this post as spam?

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


  • Mihir Ajmera 32 posts 145 karma points
    Aug 10, 2019 @ 05:24
    Mihir Ajmera
    0

    Save Dropdown list, checkbox list value in Content Page In Umbraco 8.1.0

    Hello All,

    I make one custom registration form that I use the dropdown list, checkbox list and some other textbox fields (Firstname, email, Password), etc.

    For dropdown list bind using master content node get from the back office.

    var job titles = Umbraco.Content(1216);
    

    For multiple checkbox list bind, data type(Hobbies checkbox list) value gets from the back office.

    var hobbiesCheckboxList = Services.DataTypeService.GetDataType(1495);
    

    Now I want to save the dropdown selected value and multiple checkbox list selected value in the content page.

    Dropdown selected value save as content picker value in the back office for this I do the following code.

    var registrationContent = _contentService.Create(model.FullName, 1497, "registration");
    registrationContent.SetValue("jobTitle", model.JobTitle);r
    egistrationContent.SetValue("hobbies", model.Hobbies);
    

    Using the above code I save the content page only but when clicking on display detail page then I can`t get the dropdown selected value and checkbox list selected value displays in the content page in the back office.

    So any have an idea about how to pass dropdown selected value set in the content picker, set checkbox list as multiple selected value using the surface controller method in Umbraco 8.1.0 on a submit the form.

    I do the numbers of R&D to find the solution but not get any success. So please help me solve out this as soon as possible.

    Thanks in Advance.

  • Shaishav Karnani from digitallymedia.com 354 posts 1638 karma points
    Aug 10, 2019 @ 06:28
    Shaishav Karnani from digitallymedia.com
    0

    Hi,

    You need to store UDI in the content node. Now, you cannot store ID in the content node.

    Try this:- var jobNode = Umbraco.Content(model.JobTitle); var udi = new GuidUdi("document", jobNode.GetKey()); registrationContent.SetValue("jobTitle", udi.ToString());

    This is useful link:- https://our.umbraco.com/documentation/reference/querying/Udi

  • Mihir Ajmera 32 posts 145 karma points
    Aug 10, 2019 @ 07:31
    Mihir Ajmera
    0

    Thanks, Shaishav,

    I got the solution with minor changes in your comment.

    var jobNode = Umbraco.Content(model.JobTitle); var jobUdi = new GuidUdi("document", jobNode.Key); registrationContent.SetValue("jobTitle", jobUdi.ToString());

    This work for passing the dropdown selected value in the content node picker.

    Please if you same solution for checkbox list selected then tell me so its better to implementing

    Thanks a lots.

  • Mihir Ajmera 32 posts 145 karma points
    Aug 10, 2019 @ 10:43
    Mihir Ajmera
    1

    Hi Everyone,

    Related to checkbox list selected value save in content page using below code.

    var hobbiesCheckboxList = Services.DataTypeService.GetDataType(1495);
            var checkList = model.Hobbies.TrimEnd(',').Split(',');
            var hobbiesList = ((Umbraco.Core.PropertyEditors.ValueListConfiguration)hobbiesCheckboxList.Configuration)
                .Items;
            var commStringHobbies = string.Empty;
            foreach (var selectedId in checkList)
            {
                foreach (var item in hobbiesList)
                {
                    if (Convert.ToInt32(selectedId) != item.Id) continue;
                    if (string.IsNullOrEmpty(commStringHobbies))
                        commStringHobbies = item.Value + ',';
                    else
                        commStringHobbies += item.Value + ',';
                }
            }
    
            commStringHobbies = "['" + commStringHobbies.TrimEnd(',').Replace(",", "','") + "']";
    var registrationContent = _contentService.Create(model.FullName, parentId, "registration");
                registrationContent.SetValue("hobbies", commStringHobbies);
    

    This is I think some worst solution but its work in version 8.1.0. if you get an any better solution to save checkbox list selected value then tell me so I can implement that one.

  • Ranjit J. Vaity 66 posts 109 karma points
    Feb 27, 2022 @ 10:10
    Ranjit J. Vaity
    0

    Hi Mihir,

    Your code snippet works well. Thank you very much!

    I have to spend some time understanding the exact string value required by SetValue() method.

    Following is are the dropdown values I wanted to have selected when I save the node programmatically and below is the json string I set is to...

    data from external sources

    "Draft,Cargo Condition,Port Captain Service,"

    Actual string format requried to successfully save the node

    "['Draft','Cargo Condition','Port Captain Service']"

    Thanks,

    Ranjit

Please Sign in or register to post replies

Write your reply to:

Draft