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

发布于 2024-11-02 14:37:32 字数 102 浏览 0 评论 0原文

我需要使用 nHibernate 映射来填充数据库字段中的数据,以便在 ASP.net MVC3 中进行选择...请向我发送有关如何执行此操作的示例代码..

问候 斯里维迪亚

I need to populate the data which is in the database fields using nHibernate mapping for a select in ASP.net MVC3... Please send me a sample code of how to do it..

Regards
Srividhya

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林散布 2024-11-09 14:37:32

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

public class MyViewModel
{
    public string SelectedItemId { get; set; }
    public IEnumerable<SelectListItem> Items { get; set; }
}

然后是一个将填充此视图模型的控制器(在开始时对一些值进行硬编码,以确保它有效并且您有一个模型屏幕可以向用户显示):

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            Items = new[] 
            {
                new SelectListItem { Value = "1", Text = "item 1" },
                new SelectListItem { Value = "2", Text = "item 2" },
                new SelectListItem { Value = "3", Text = "item 3" },
            }
        };
        return View(model);
    }
}

最后是一个视图:

@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 HomeController : Controller
{
    private readonly IItemsRepository _repository;
    public HomeController(IItemsRepository repository)
    {
        _repository = repository;
    }

    public ActionResult Index()
    {
        var items = _repository.GetItems();
        var model = new MyViewModel
        {
            Items = items.Select(item => new SelectListItem
            {
                Value = item.Id.ToString(),
                Text = item.Name
            })
        };
        return View(model);
    }
}

好的,我们正在一点一点地取得进展。现在您可以为此控制器操作编写单元测试。

下一步是实现此存储库:

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

最后一步是指示您的依赖项注入框架将存储库的正确实现传递给 HomeController。例如,如果您使用 Ninject 您需要执行的所有操作要做的就是编写一个配置内核的模块:

public class RepositoriesModule : StandardModule 
{
    public override void Load() 
    {
        Bind<IItemsRepository>().To<ItemsRepositoryNHibernate>();
    }
}

You could start by defining a view model:

public class MyViewModel
{
    public string SelectedItemId { get; set; }
    public IEnumerable<SelectListItem> Items { get; set; }
}

then a controller which will populate this view model (hardcode some values at the beginning just to make sure that it works and you have a mockup screens to show to your users):

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            Items = new[] 
            {
                new SelectListItem { Value = "1", Text = "item 1" },
                new SelectListItem { Value = "2", Text = "item 2" },
                new SelectListItem { Value = "3", Text = "item 3" },
            }
        };
        return View(model);
    }
}

and finally a view:

@model MyViewModel
@Html.DropDownListFor(
    x => x.SelectedItemId, 
    new SelectList(Model.Items, "Value", "Text")
)

The next step could consist into defining a model, setting the mapping for this model, a repository allowing you to fetch the model with NHibernate and finally call this repository in the controller action and map the returned model to the view model I used in the example:

Model:

public class Item
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}

Repository:

public interface IItemsRepository
{
    IEnumerable<Item> GetItems();
}

and now the controller becomes:

public class HomeController : Controller
{
    private readonly IItemsRepository _repository;
    public HomeController(IItemsRepository repository)
    {
        _repository = repository;
    }

    public ActionResult Index()
    {
        var items = _repository.GetItems();
        var model = new MyViewModel
        {
            Items = items.Select(item => new SelectListItem
            {
                Value = item.Id.ToString(),
                Text = item.Name
            })
        };
        return View(model);
    }
}

OK, we are making progress little by little. Now you can write unit tests for this controller action.

The next step would be to implement this repository:

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

and the last step is to instruct your dependency injection framework to pass the correct implementation of the repository to the HomeController. For example if you use Ninject all you need to do is to write a module that will configure the kernel:

public class RepositoriesModule : StandardModule 
{
    public override void Load() 
    {
        Bind<IItemsRepository>().To<ItemsRepositoryNHibernate>();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文