London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!

SelectionFactory set Initial/Default value and display it's placeholder in select/dropdown box

Vote:
 

Hi all,

I found this topic here - https://world.episerver.com/forum/developer-forum/-Episerver-75-CMS/Thread-Container/2014/9/Setting-a-ISelectItem-as-selected/ - but none of the solutions solves my problem.

I have this property on a page, which uses custom Selection Factory.

And I want to set initial value and display it's placeholder (in admin view, under "All properties" view while creating new page) here in a select box. I do it like this:

// CustomSelectionFactory.cs:

public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
{
    List<SelectItem> result = new List<SelectItem>()
    {
        new SelectItem() { Text = "All", Value = null }
    };

    List<CustomSelectItem> customSelectItems = new List<CustomSelectItem>();

    switch (metadata.PropertyName)
    {
        case "DocumentCountry":
            customSelectItems = Api.GetCustomSelectItems(1).Result; 
            break;
        case "DocumentLanguage":
            customSelectItems = Api.GetCustomSelectItems(2).Result; 
            result.RemoveAll(x => x.Text == "All");
            metadata.InitialValue = "1";
            break;
    }

    foreach (CustomSelectItems customSelectItem in customSelectItems)
    {
        result.Add(new SelectItem()
        {
            Text = customSelectItem .Name,
            Value = customSelectItem .Id.ToString()
        });
    }

    return result;
}

// CustomPage.cs

[Display(Name = "Document Country")]
[SelectOne(SelectionFactoryType = typeof(CustomSelectionFactory))]
public virtual string DocumentCountry { get; set; }

[Display(Name = "Document Language")]
[SelectOne(SelectionFactoryType = typeof(CustomSelectionFactory))]
public virtual string DocumentLanguage { get; set; }

Help appreciated!

#208399
Edited, Oct 23, 2019 11:20
Vote:
 

Hi,

You can set the default value.

public override void SetDefaultValues(ContentType contentType)
{
    base.SetDefaultValues(contentType);
 
    //Set values here
 
}
#208402
Oct 23, 2019 12:15
fuji - Oct 23, 2019 13:50
Ok, now this worked (even without setting "metadata.InitialValue = "1";" in "CustomSelectionFactory.cs", the issue was probably because "DocumentLanguage" property was of type "int?". Thank you!
Vote:
 

@Ravindra S. Rathore,

I think I have tried it and it partly works - on publish it passes further the default value, but the placeholder of selectbox is still empty

#208403
Oct 23, 2019 12:17
Vote:
 

Placeholder in CMS or on the front-end site?

#208405
Oct 23, 2019 12:31
fuji - Oct 23, 2019 12:42
In admin view, under "All properties" view while creating new page, so I guess it's CMS? :D
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.