user2067567
user2067567

Reputation: 3813

Mapping a POCO Class to MVC Model

I am new to MVC as well as EF. Following some Nice Tutorials I finally created my POCO Classes.

I am trying to create a MVC Model with the help of POCO Classes in a layered architecture. Where My POCO class sits in Class Library project called Entities.

And my MVC4 application is a Web Project which references Entities.

I query database and have content in Entities and want to map 3-4 POCO classes to a single Model so i can create a strongly typed view.

Am not sure how to proceed. Assist me on this.

Upvotes: 1

Views: 4643

Answers (2)

Derek Beattie
Derek Beattie

Reputation: 9478

Check out the ASP.NET MVC in Action book.. It has an entire chapter dedicated to this topic. They suggest and show how to use AutoMapper to map domain entities to ViewModels.

I also have an example here.

In the BootStrapper:

 Mapper.CreateMap<Building, BuildingDisplay>()
                .ForMember(dst => dst.TypeName, opt => opt.MapFrom(src => src.BuildingType.Name)); 

In the controller:

  public ActionResult Details(int id)
        {
            var building = _db.Buildings.Find(id);

            if (building == null)
            {
                ViewBag.Message = "Building not found.";
                return View("NotFound");
            }

            var buildingDisplay = Mapper.Map<Building, BuildingDisplay>(building);
            buildingDisplay.BinList = Mapper.Map<ICollection<Bin>, List<BinList>>(building.Bins);

            return View(buildingDisplay);            
        }    

Upvotes: 2

Idrees Khan
Idrees Khan

Reputation: 7752

i have answer such type of question once before, if you want to see it , here is the link

http://stackoverflow.com/questions/15432246/creating-a-mvc-viewmodels-for-my-data/15436044#15436044

But inform me back if you need more help :D here is your viewmodel

public class CategoryViewModel
{
    [Key]
    public int CategoryId { get; set; }
    [Required(ErrorMessage="* required")]
    [Display(Name="Name")]
    public string CategoryName { get; set; }
    [Display(Name = "Description")]
    public string CategoryDescription { get; set; }
    public ICollection<SubCategory> SubCategories { get; set; }
}

Now for mapping use projection in linq;

public List<CategoryViewModel> GetAllCategories()
{
    using (var db =new Entities())
    {
        var categoriesList = db .Categories
            .Select(c => new CategoryViewModel() // here is projection to your view model
            {
                CategoryId = c.CategoryId,
                CategoryName = c.Name,
                CategoryDescription = c.Description
            });
        return categoriesList.ToList<CategoryViewModel>();
    };
 }

now your controller;

ICategoryRepository _catRepo;
    public CategoryController(ICategoryRepository catRepo)
    {
        //note that i have also used the dependancy injection. so i'm skiping that
        _catRepo = catRepo;
    }
    public ActionResult Index()
    {
        //ViewBag.CategoriesList = _catRepo.GetAllCategories();
           or
        return View(_catRepo.GetAllCategories());
    }

And lastly your view;

@model IEnumerable<CategoryViewModel>
@foreach (var item in Model)
{
    <h1>@item.CategoryName</h1>
}

Upvotes: 2

Related Questions