2012-12-29 50 views
0

使用Orchard CMS,我正在處理記錄和零件代理,但無法弄清楚如何將其保存到數據庫中。事實上,我承認我甚至不知道如何讓我試圖保存的物品進入這種範式。我本來用enum的供選擇:如何在Orchard CMS中存儲逗號分隔列表?

MyEmum.cs

public enum Choices { Choice1, Choice2, Choice3, Choice4 } 

MyRecord.cs

public virtual string MyProperty { get; set; } 

MyPart.cs

public IEnumerable<string> MyProperty 
{ 
    get 
    { 
     if (String.IsNullOrWhiteSpace(Record.MyProperty)) return new string[] { }; 
     return Record 
      .MyProperty 
      .Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries) 
      .Select(r => r.Trim()) 
      .Where(r => !String.IsNullOrEmpty(r)); 
    } 
    set { Record.MyProperty = value == null ? null : String.Join(",", value); } 
} 

現在,在我的服務類,我試過如:

public MyPart Create(MyPartRecord record) 
{ 
    MyPart part = Services.ContentManager.Create<MyPart>("My"); 
    ... 
    part.MyProperty = record.MyProperty; //getting error here 
    ... 
    return part; 
} 

不過,我收到以下錯誤:Cannot implicitly convert 'string' to System.Collections.Generic.IEnumerable<string>'

從本質上講,我試圖從一個CheckBoxList的(一個或多個選擇)作爲一個數據庫中的逗號分隔的列表保存的選擇。

這甚至沒有讓我知道如何使用enum的問題。有什麼想法嗎?

對於一些背景:

我明白,處理這種關係的適當方法是創建一個單獨的表,並使用IList<MyEnum>。但是,這是一個簡單的列表,我不打算通過編輯進行操作(實際上,在這種情況下沒有使用驅動程序,因爲我在前端使用控制器和路由處理此列表)。我只是捕獲數據並重新顯示在管理視圖中以用於統計/歷史目的。我甚至可以考慮擺脫部分(考慮下面的帖子:Bertrand's Blog Post

回答

0

它應該是:

part.MyProperty = new[] {"foo", "bar"}; 

例如,該部分的setter方法上記錄的屬性的值存儲爲一個逗號。分隔字符串,將得到持續到DB。

如果你想使用枚舉值,你應該使用ParseToString的API,.NET提供枚舉。

相關問題