Serenity: Set default value of a drop down for lookup field

Created on 4 Sep 2018  路  9Comments  路  Source: serenity-is/Serenity

I have 2 lookup fields Country / State and I would like them to default to a selection.

I have had a look here
https://github.com/volkanceylan/Serenity/wiki/LookupFilteringAttribute-%5BColumns%5D

I do not wish to filter the selection - I want to preselect what will be the most used selection.

Most helpful comment

DefaultValue attribute

All 9 comments

I looked at this - but this is for quick filter on add new entity - I would like to set the selection.
https://github.com/volkanceylan/Serene/blob/master/Serene/Serene.Web/Modules/BasicSamples/Grids/InitialValuesForQuickFilters/InitialValuesForQuickFilters.ts

Hi @stixoffire , we make this attribute for default values..

 public class DefaultValueFromTraductionAttribute : System.ComponentModel.DefaultValueAttribute
    {
        public DefaultValueFromTraductionAttribute([CallerMemberName] string propertyName = null, [CallerFilePath] string classFile = null) : base(null)
        {
            var path = classFile.Split('\\');
            var entity = path[path.Length - 1].Split('.')[0].Replace("Row", "").Replace("Form", "").Replace("Columns", "");
            var textForLook = $"Db.{entity}.{propertyName}.DefaultValue";
            var defaultValue = LocalText.TryGet(textForLook);

            if (string.IsNullOrEmpty(defaultValue))
            {
                try
                {
                    var registry = Dependency.Resolve<Serenity.Abstractions.ILocalTextRegistry>();
                    registry.Add(LocalText.InvariantLanguageID, textForLook, string.Empty );  
                }
                catch
                {
                }
            }

            if (!string.IsNullOrEmpty(defaultValue))
                SetValue(defaultValue);
        }

        public DefaultValueFromTraductionAttribute(string Value, [CallerMemberName] string propertyName = null, [CallerFilePath] string classFile = null) : base(Value)
        {
            var path = classFile.Split('\\');
            var entity = path[path.Length - 1].Split('.')[0].Replace("Row", "").Replace("Form", "").Replace("Columns", "");
            var textForLook = $"Db.{entity}.{propertyName}.DefaultValue";
            var defaultValue = LocalText.TryGet(textForLook);

            if (string.IsNullOrEmpty(defaultValue))
            {
                try
                {
                    var registry = Dependency.Resolve<Serenity.Abstractions.ILocalTextRegistry>();
                    registry.Add(LocalText.InvariantLanguageID, textForLook, string.Empty); 
                }
                catch
                {
                }
            }

            if (!string.IsNullOrEmpty(defaultValue))
                SetValue(defaultValue);
            else
            {
                if (!string.IsNullOrEmpty(Value))
                {
                    SetValue(Value);
                }
            }
        }
    }

@rcruzfalcon
Thank you for sharing that ..
Your code looks like you grab the database default to set the value for the item; However I am simply trying to set the drop down to a specific 'selection' - as there is no default in the database for the property - it is an enumeration table - for example states, I want a specific state to be set when adding contact information.
Is there anything like that - I am assuming I need to do this in the .ts file - Jquery I guess ..

Hi, this code read from "traslator" the code Value for show this one seleted. the user can set this value like traslator Caption.
Otherwise, if you need preselected must be set the propertie with any Value of source of selection in the newEntity method.

I'm trying to do the same thing and I'm confused by this answer and documentation. I have a table called NetworkDevice with a Boolean field called 'Disposed'. I can make a dropdown filter for it like this in NetworkDeviceColumn.cs-

[LookupEditor(typeof(Lookups.NetworkDeviceDisposedLookup)), QuickFilter(CssClass = "hidden-xs")] public Boolean Disposed { get; set; }

And that works fine. Now how do I make it default to 'False'?

just set it somewhere in AfteruentityUdate, or constructor... whatever suits you

@ga5tan AfterUpdate is too late .. the DropDown is shown and it should default to the default selection for example State DefaultValue = State_where most of our customers are.

@stixoffire yeah, whatever works for u, there is zillion events. BeforeLoadEntity, afterLoadEntity, constructor, u name it. I just gave u example, you should choose what suits u.

DefaultValue attribute

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JohnRanger picture JohnRanger  路  3Comments

GitHubOrim picture GitHubOrim  路  3Comments

john20xdoe picture john20xdoe  路  3Comments

Pinellus picture Pinellus  路  3Comments

moostafaa picture moostafaa  路  3Comments