开发者

NHibernate IList as a Drop Down datasource?

I'm attempting to populate a drop down from an Nhibernate object, but having problems getting it working properly. I've created the object and mapped it using the following

public class Status
{
    public virtual int StatusId { get; set; }
    public virtual string StatusName { get; set; }

}

...

<class name="CM.Core.Status, CM.Core" table="refStatus">
    <id name="StatusId" column="statusId" type="Int32">
    <generator class="native"></generator>
    </id>
<property name="StatusName" column="status" type="string"/>
</class>

I then populate it using

    public IList<Status> GetStatuses()
    {
        return _session.CreateQuery("select s from Status s")
            .List<Status>();
    }

Finally, I populate my dropdown using

        IList<Status> status = _provider.GetStatuses();
        ddlStatus.DataSource = status;
        ddlStatus.DataBind();

However, it populates the values and text with my class name instead of the status values the correct numb开发者_开发百科er of times

CM.Core.Status

CM.Core.Status

CM.Core.Status

CM.Core.Status

Is IList the incorrect collection type to be used in this situation? Should I be casting it as something different? Is there a way to access the class properties prior to databind?


You need to specify the data text fields and data value fields.

   ddlStatus.DataTextField = "StatusName";
   ddlStatus.DataValueField = "StatusId";


You need to set

ddl.DisplayMember = "StatusName";
ddl.ValueMemeber = "StatusId";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜