0

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

Vidya
  • 126
  • 2
  • 4
  • 16

1 Answers1

5

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>();
    }
}
Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928