使用nHibernate使用数据库中的数据填充ASP.net MVC3中的select

使用nHibernate使用数据库中的数据填充ASP.net MVC3中的select,asp.net,asp.net-mvc-3,select,Asp.net,Asp.net Mvc 3,Select,我需要为ASP.net MVC3中的选择使用nHibernate映射填充数据库字段中的数据。。。请给我一个如何做的示例代码 问候 Srividhya您可以从定义视图模型开始: public class MyViewModel { public string SelectedItemId { get; set; } public IEnumerable<SelectListItem> Items { get; set; } } 最后是一个观点: @model MyVi

我需要为ASP.net MVC3中的选择使用nHibernate映射填充数据库字段中的数据。。。请给我一个如何做的示例代码

问候
Srividhya

您可以从定义视图模型开始:

public class MyViewModel
{
    public string SelectedItemId { get; set; }
    public IEnumerable<SelectListItem> Items { get; set; }
}
最后是一个观点:

@model MyViewModel
@Html.DropDownListFor(
    x => x.SelectedItemId, 
    new SelectList(Model.Items, "Value", "Text")
)
下一步可能包括定义模型、设置此模型的映射、使用NHibernate获取模型的存储库,最后在控制器操作中调用此存储库,并将返回的模型映射到示例中使用的视图模型:

型号:

public class Item
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}
存储库:

public interface IItemsRepository
{
    IEnumerable<Item> GetItems();
}
public class ItemsRepositoryNHibernate : IItemsRepository
{
    public IEnumerable<Item> GetItems()
    {
        throw new NotImplementedException(
            "Out of the scope for this question. Checkout the NHibernate manual"
        );
    }
}
好的,我们正在一点一点地取得进展。现在您可以为此控制器操作编写单元测试

下一步是实现此存储库:

public interface IItemsRepository
{
    IEnumerable<Item> GetItems();
}
public class ItemsRepositoryNHibernate : IItemsRepository
{
    public IEnumerable<Item> GetItems()
    {
        throw new NotImplementedException(
            "Out of the scope for this question. Checkout the NHibernate manual"
        );
    }
}

可以从定义视图模型开始:

public class MyViewModel
{
    public string SelectedItemId { get; set; }
    public IEnumerable<SelectListItem> Items { get; set; }
}
最后是一个观点:

@model MyViewModel
@Html.DropDownListFor(
    x => x.SelectedItemId, 
    new SelectList(Model.Items, "Value", "Text")
)
下一步可能包括定义模型、设置此模型的映射、使用NHibernate获取模型的存储库,最后在控制器操作中调用此存储库,并将返回的模型映射到示例中使用的视图模型:

型号:

public class Item
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}
存储库:

public interface IItemsRepository
{
    IEnumerable<Item> GetItems();
}
public class ItemsRepositoryNHibernate : IItemsRepository
{
    public IEnumerable<Item> GetItems()
    {
        throw new NotImplementedException(
            "Out of the scope for this question. Checkout the NHibernate manual"
        );
    }
}
好的,我们正在一点一点地取得进展。现在您可以为此控制器操作编写单元测试

下一步是实现此存储库:

public interface IItemsRepository
{
    IEnumerable<Item> GetItems();
}
public class ItemsRepositoryNHibernate : IItemsRepository
{
    public IEnumerable<Item> GetItems()
    {
        throw new NotImplementedException(
            "Out of the scope for this question. Checkout the NHibernate manual"
        );
    }
}