Asp.net mvc 3 试图保存逗号分隔的列表

Asp.net mvc 3 试图保存逗号分隔的列表,asp.net-mvc-3,nhibernate,Asp.net Mvc 3,Nhibernate,正在尝试将复选框列表中的选择保存为数据库中的逗号分隔列表(字符串)(选择了一个或多个选项)。我使用代理将其另存为字符串,因为否则我必须在DB中为关系创建单独的表-对于这个简单的场景,这项工作不值得,我希望我可以将其转换为字符串,并避免这种情况 复选框列表使用enum作为其选项: public enum Selection { Selection1, Selection2, Selection3 } 这并不复杂,但我使用[Display(Name=“Choice 1”

正在尝试将复选框列表中的选择保存为数据库中的逗号分隔列表(
字符串
)(选择了一个或多个选项)。我使用代理将其另存为
字符串
,因为否则我必须在DB中为关系创建单独的表-对于这个简单的场景,这项工作不值得,我希望我可以将其转换为
字符串
,并避免这种情况

复选框列表使用
enum
作为其选项:

public enum Selection 
{ 
    Selection1,
    Selection2,
    Selection3
}
这并不复杂,但我使用
[Display(Name=“Choice 1”)]
和一个扩展类在UI上显示友好的内容。不确定我是否可以保存该
字符串
,而不仅仅是
枚举
,尽管我认为如果我保存为
枚举
,在某个确认页面的UI上“显示”友好字符串对我来说并不是什么大问题

这是在数据库中保存
字符串的“Record”类:

public virtual string MyCheckBox { get; set; }
这就是“代理”,它是我找到的一些示例,但不是直接处理
enum
,它使用
IEnumerable
(或者应该是
IEnumerable
):

但是我得到了一个错误:

无法将“字符串”隐式转换为System.Collections.Generic.IEnumerable

我不知道使用API中的
Parse
ToString
来获取枚举值是否可能或更好

我知道这样做会将我在
(“”)
中输入的任何内容存储到数据库中,因此这只是一个如何克服错误的问题(或者,如果有其他选择):


我对这些东西不太在行,我一直在挖啊挖,想找到一个解决办法。非常感谢您的帮助。

您可以使用自定义用户类型来完成此操作。下面的示例在类上使用
ISet
,并将值存储为分隔字符串

[Serializable]
public class CommaDelimitedSet : IUserType
{
    const string delimiter = ",";

    #region IUserType Members

    public new bool Equals(object x, object y)
    {
        if (ReferenceEquals(x, y))
        {
            return true;
        }
        var xSet = x as ISet<string>;
        var ySet = y as ISet<string>;
        if (xSet == null || ySet == null)
        {
            return false;
        }
        // compare set contents
        return xSet.Except(ySet).Count() == 0 && ySet.Except(xSet).Count() == 0;
    }

    public int GetHashCode(object x)
    {
        return x.GetHashCode();
    }

    public object NullSafeGet(IDataReader rs, string[] names, object owner)
    {
        var outValue = NHibernateUtil.String.NullSafeGet(rs, names[0]) as string;
        if (string.IsNullOrEmpty(outValue))
        {
            return new HashSet<string>();
        }
        else
        {
            var splitArray = outValue.Split(new[] {Delimiter}, StringSplitOptions.RemoveEmptyEntries);
            return new HashSet<string>(splitArray);
        }
    }

    public void NullSafeSet(IDbCommand cmd, object value, int index)
    {
        var inValue = value as ISet<string>;
        object setValue = inValue == null ? null : string.Join(Delimiter, inValue);
        NHibernateUtil.String.NullSafeSet(cmd, setValue, index);
    }

    public object DeepCopy(object value)
    {
        // return new ISet so that Equals can work
        // see http://www.mail-archive.com/nhusers@googlegroups.com/msg11054.html
        var set = value as ISet<string>;
        if (set == null)
        {
            return null;
        }
        return new HashSet<string>(set);
    }

    public object Replace(object original, object target, object owner)
    {
        return original;
    }

    public object Assemble(object cached, object owner)
    {
        return DeepCopy(cached);
    }

    public object Disassemble(object value)
    {
        return DeepCopy(value);
    }

    public SqlType[] SqlTypes
    {
        get { return new[] {new SqlType(DbType.String)}; }
    }

    public Type ReturnedType
    {
        get { return typeof(ISet<string>); }
    }

    public bool IsMutable
    {
        get { return false; }
    }

    #endregion
}
[可序列化]
公共类CommaDelimitedSet:IUserType
{
常量字符串分隔符=“,”;
#区域类型成员
公共新布尔等于(对象x、对象y)
{
if(ReferenceEquals(x,y))
{
返回true;
}
var xSet=x作为ISet;
var ySet=y作为ISet;
if(xSet==null | | ySet==null)
{
返回false;
}
//比较集合内容
返回xSet.Except(ySet.Count()==0&&ySet.Except(xSet.Count()==0;
}
公共int GetHashCode(对象x)
{
返回x.GetHashCode();
}
公共对象NullSafeGet(IDataReader rs,字符串[]名称,对象所有者)
{
var outValue=NHibernateUtil.String.NullSafeGet(rs,name[0])作为字符串;
if(string.IsNullOrEmpty(outValue))
{
返回新的HashSet();
}
其他的
{
var splitArray=outValue.Split(新[]{Delimiter},StringSplitOptions.RemoveEmptyEntries);
返回新的HashSet(splitArray);
}
}
public void NullSafeSet(IDbCommand cmd,对象值,int索引)
{
var inValue=作为ISet的值;
对象setValue=inValue==null?null:string.Join(分隔符,inValue);
NHibernateUtil.String.NullSafeSet(cmd、setValue、index);
}
公共对象深度复制(对象值)
{
//返回新的ISet以便Equals可以工作
//看http://www.mail-archive.com/nhusers@googlegroups.com/msg11054.html
var set=作为ISet的值;
if(set==null)
{
返回null;
}
返回新的HashSet(set);
}
公共对象替换(对象原始、对象目标、对象所有者)
{
归还原件;
}
公共对象集合(对象缓存,对象所有者)
{
返回DeepCopy(缓存);
}
公共对象(对象值)
{
返回DeepCopy(值);
}
公共SqlType[]SqlTypes
{
获取{returnnew[]{newsqltype(DbType.String)};}
}
公共类型返回类型
{
获取{return typeof(ISet);}
}
公共布尔可换
{
获取{return false;}
}
#端区
}
映射文件中的用法:

Map(x => x.CheckboxValues.CustomType<CommaDelimitedSet>();
Map(x=>x.CheckboxValues.CustomType();

你的代码的getter在“.”上拆分,setter在“,”上合并。对吗?抱歉,这是一个排版代码OK,因此你的代理的MyCheckBox是IEnumerable,而Record.MyCheckBox是一个字符串。这两个在赋值时不兼容。我不确定我是否理解“代理”的用途这里。@SrikanthVenugopalan,因为我无法在DB中保存MyCheckBox,因为我需要为关系创建单独的关联表。我希望我可以将其转换为字符串并以这种方式保存,而不是键入大量代码来创建关系。这个答案说明了这一点MVCContrib如何帮助绑定复选框列表..也许您可以在此基础上构建?
proxy.MyCheckBox = new[] {"foo", "bar"};
[Serializable]
public class CommaDelimitedSet : IUserType
{
    const string delimiter = ",";

    #region IUserType Members

    public new bool Equals(object x, object y)
    {
        if (ReferenceEquals(x, y))
        {
            return true;
        }
        var xSet = x as ISet<string>;
        var ySet = y as ISet<string>;
        if (xSet == null || ySet == null)
        {
            return false;
        }
        // compare set contents
        return xSet.Except(ySet).Count() == 0 && ySet.Except(xSet).Count() == 0;
    }

    public int GetHashCode(object x)
    {
        return x.GetHashCode();
    }

    public object NullSafeGet(IDataReader rs, string[] names, object owner)
    {
        var outValue = NHibernateUtil.String.NullSafeGet(rs, names[0]) as string;
        if (string.IsNullOrEmpty(outValue))
        {
            return new HashSet<string>();
        }
        else
        {
            var splitArray = outValue.Split(new[] {Delimiter}, StringSplitOptions.RemoveEmptyEntries);
            return new HashSet<string>(splitArray);
        }
    }

    public void NullSafeSet(IDbCommand cmd, object value, int index)
    {
        var inValue = value as ISet<string>;
        object setValue = inValue == null ? null : string.Join(Delimiter, inValue);
        NHibernateUtil.String.NullSafeSet(cmd, setValue, index);
    }

    public object DeepCopy(object value)
    {
        // return new ISet so that Equals can work
        // see http://www.mail-archive.com/nhusers@googlegroups.com/msg11054.html
        var set = value as ISet<string>;
        if (set == null)
        {
            return null;
        }
        return new HashSet<string>(set);
    }

    public object Replace(object original, object target, object owner)
    {
        return original;
    }

    public object Assemble(object cached, object owner)
    {
        return DeepCopy(cached);
    }

    public object Disassemble(object value)
    {
        return DeepCopy(value);
    }

    public SqlType[] SqlTypes
    {
        get { return new[] {new SqlType(DbType.String)}; }
    }

    public Type ReturnedType
    {
        get { return typeof(ISet<string>); }
    }

    public bool IsMutable
    {
        get { return false; }
    }

    #endregion
}
Map(x => x.CheckboxValues.CustomType<CommaDelimitedSet>();