我如何保存在果园CMS一个逗号分隔的列表? [英] How do I store a comma-separated list in Orchard CMS?

查看:89
本文介绍了我如何保存在果园CMS一个逗号分隔的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用CMS果园,我处理的一个记录,并且部分代理,但无法弄清楚如何将其保存到数据库。事实上,我承认,我甚至不知道如何让我想保存到这种模式的项目。我本来是用枚举的选择为:

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

不过,我收到以下错误:无法隐式转换字符串到System.Collections.Generic.IEnumerable&LT;串GT;

从本质上讲,我试图从的CheckBoxList(一项或多项选择),选择保存在一个数据库中的逗号分隔的列表。

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?

有关的一些背景:

据我了解,处理这种关系的适当方法是创建一个单独的表,并使用的IList&LT; MyEnum&GT; 。然而,这是我不打算与编辑操作(其实没有司机在此方案中使用,因为我处理这个问题的前端带有一个控制器和路由)一个简单的列表。我只是采集数据并进行统计/历史目的的管理视图重新显示它。我甚至可以考虑摆脱部分(考虑以下职位:的伯特兰的博客文章

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.

推荐答案

它应该是:

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

例如。该器件的setter将值存储在该记录的属性作为一个逗号分隔的字符串,这将让坚持到数据库。

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.

如果你想使用枚举值,你应该使用解析的ToString 的API,.NET提供枚举。

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

这篇关于我如何保存在果园CMS一个逗号分隔的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆