2017-08-02 132 views

回答

3

默認下拉不支持默認值

沒有達到你想要

  1. 創建自己的下拉菜單的數據類型(什麼的雙向或使用別人所做的插件 - 我不確定哪一個支持它,但也許看看nuPickers

  2. 使用Web API處理器攔截獲取內容價值的呼喚 - 並設置默認值,你的財產,如果它爲空(NULL)

下面

是一些未測試的代碼:

首先將創建Web API處理器

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; 
    } 
} 

然後在開始的活動,其註冊

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

您的問題,也許你不知道你的prevalueid - 你可以看看它在分貝或者你可以使用的數據類型的服務來獲取數據類型prevalues然後決定哪些把默認