How do I store a comma-separated list in Orchard CMS?

北慕城南 提交于 2019-12-13 02:56:33

问题


Using Orchard CMS, I am dealing with a record and a part proxy, but cannot figure out how to save it into the DB. In fact, I confess I don't even know how to get the items I'm trying to save into this paradigm. I was originally using enum's for choices:

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

Now, in my service class, I tried something like:

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

However, I am getting the following error: Cannot implicitly convert 'string' to System.Collections.Generic.IEnumerable<string>'

Essentially, I am trying to save choices from a checkboxlist (one or more selections) as a comma-separated list in the DB.

And this doesn't even get me over the problem of how do I use the enum. Any thoughts?

For some background:

I understand that the appropriate way to handle this relationship would be to create a separate table and use IList<MyEnum>. However, this is a simple list that I do not intend to manipulate with edits (in fact, no driver is used in this scenario as I handle this on the front-end with a controller and routes). I am just capturing data and redisplaying it in the Admin view for statistical/historical purposes. I may even consider getting rid of the Part (considering the following post: Bertrand's Blog Post.


回答1:


It should be:

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

for example. The part's setter will store the value on the record's property as a comma-separated string, which will get persisted into the DB.

If you want to use enum values, you should use the Parse and ToString APIs that .NET provide on Enum.



来源:https://stackoverflow.com/questions/14082926/how-do-i-store-a-comma-separated-list-in-orchard-cms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!