Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign default value for dropdown list in Umbraco?

I have created custom data type based on built-in dropdown list, but cannot figure out how to specify default value for the list. The default value is always blank:

enter image description here

like image 897
Ivan Studenikin Avatar asked Oct 14 '25 14:10

Ivan Studenikin


2 Answers

The default dropdown does not support default value

There is two way of achieving what you want

  1. create your own dropdown datatype (or use a plugin someone else has made - I am not sure which one support it, but maybe have a look at nuPickers )

    • since it is your custom made you can control it. More about how to create one checkout doc Tutorial - Creating a property editor
  2. use a web api handler to intercept the call of getting the content value - and set a default value to your property if it is empty (null)

below is some un-tested code:

first create the web api handler

public class SetDropdownDefaultHandler : DelegatingHandler
{
    protected override async Task<HttpResponseMessage> SendAsync
            (HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var response = await base.SendAsync(request, cancellationToken);

        var url = request?.RequestUri?.AbsolutePath.ToLower;

        // only process when a create (getempty) or editing a specific content (getbyid) 
        if (url == "/umbraco/backoffice/umbracoapi/content/getempty"
            || url == "/umbraco/backoffice/umbracoapi/content/getbyid")
        {
            var content = (ObjectContent)response.Content;
            var data = content?.Value as PagedResult<ContentItemBasic<ContentPropertyBasic, IContent>>;

            if (data?.Items != null)
            {
                var tempResult = data?.Items?.ToList();

                foreach (var item in tempResult)
                {
                    foreach (var prop in item?.Properties?.Where(p => p?.Editor == "Umbraco.DropDown"))
                    {
                        var propStr = prop.Value?.ToString();
                        if (!propStr.IsNullOrWhiteSpace())
                        {
                            // set your default value if it is empty
                            prop.Value = "your default option prevalue id";
                        }
                    }
                }

                data.Items = tempResult;
            }
        }

        return response;
    }
}

then register it at started event

public class UmbracoEvent : ApplicationEventHandler
{
  protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
  {
    GlobalConfiguration.Configuration.MessageHandlers.Add(new SetDropdownDefaultHandler());
  }
}

your problem maybe you don't know your prevalueid - you can look it up in db or you could use datatype service to get the datatype prevalues then decide which to put as default

like image 177
Alan Tsai Avatar answered Oct 18 '25 06:10

Alan Tsai


Look at: FieldType.DropDownList in the fieldTypes folder.

Replace:<option value=""></option>

With:

var settings = Model.AdditionalSettings; <option value="">@settings["DefaultValue"]</option>

Then ensure you set the default value property in your dropdown list in the Umbraco Forms backoffice for the given form

enter image description here enter image description here

like image 36
Damo Avatar answered Oct 18 '25 06:10

Damo