Performing a test task for one company (the conditions of which were not to use Entety and other ORM), I ran into the problem: how to display related models in the most optimal and correct way.

There are for example two classes: Authors :

 public partial class Author { [ScaffoldColumn(false)] public int id { get; set; } [Required, StringLength(50), Display(Name = "Firstname")] public string firstname { get; set; } [Required, StringLength(50), Display(Name = "Lastname")] public string lastname { get; set; } [Display(Name = "Date of birth"), DataType(DataType.Date)] public DateTime dateOfBirth { get; set; } } 

And Book , which has an array of IDs on the authors idAuthors

 public class Book { [ScaffoldColumn(false)] public int id { get; set; } [Required, StringLength(100), Display(Name = "Name book")] public string name { get; set; } [StringLength(4000), Display(Name = "Description"), DataType(DataType.MultilineText)] public string description { get; set; } [Display(Name = "Pages")] public short countPages { get; set; } [Required, Display(Name = "Publication"), DataType(DataType.Date)] public DateTime dateOfPublication { get; set; } [Required, Display(Name = "Authors")] public int[] idAuthors { get; set; } [Required, Display(Name = "Count")] public int count { get; set; } } 

I did it like this. In the controller in ViewBag passed the collection of all authors

 public ActionResult Index(int? page) { IEnumerable<Book> books = repository.Books.GetAll(); if (booksInfo == null) { return HttpNotFound(); } ViewBag.AllAuthors = repository.Authors.GetAll(); int pageSize = 5; int pageNumber = (page ?? 1); return View(books.ToPagedList(pageNumber, pageSize)); } 

And in View when checking them out in a loop

 @foreach (var author in (List<TaskLibrary.Models.Author>)ViewBag.AllAuthors) { if (item.idAuthors.Contains(author.id)) { <p> @Html.DisplayFor(modelItem => author.NameFull) </p> } } 

But I do not think that it is right ...

    0