MVC How to Read Related Data

I know this question has been asked quite a lot in all sort of different ways but I am still struggling.

I have two classes;

Author
public int authorID {get; set;}
public int authorName {get; set;}
public int category_id {get;set}
AuthorCategory
public int category_id {get;set;}
public int category_name {get;set;}
public virtual ICollection<Author> Authors {get; set;}

On one page, I want to display the list of Authors with their respective category names next to their names. I managed to do this by returning

db.Authors.Include(c => c.AuthorCategory)

to my view from the controller but that just doesn't seem right. Is there a better way of doing this? I looked into something called ViewModels but did not quite understand how that would help.

Also, for example in a "New Author" form, I have to display the Author Categories in a dropdown. Will I use the same model for this?

I know my questions are quite simple but coming from a web forms background, I am having a little difficulty figuring out the logic of MVC. Any help is appreciated!

Answers


The learning curve you're on is steep, but once you get to the top you'll be fine. Then you'll start learning AngularJS and wonder why you ever went down the ASP.Net MVC route. :)

If you are consuming a single model in your View then you don't need to use a ViewModel. There's nothing wrong with calling db.Authors.Include(c => c.AuthorCategory), that's exactly what you need to do to get past the lazy loading of EF (I'm guessing you're using EF).

Now you want to include the Author Categories in a dropdown and this is where the idea of a ViewModel comes in because you are no longer consuming just a single model in your View. You need both Author and a list of available AuthorCategory values.

You could create your AuthorViewModel.cs class as something like:

public class AuthorViewModel
{
    public Author Author { get; set; }
    public List<AuthorCategory> AuthorCategories { get; private set; }

    public AuthorViewModel() {}

    public AuthorViewModel(int id)
    {
        using(var db = new myDbContext())
        {
            this.Author = db.Authors.SingleOrDefault(a => a.AuthorId == id);
            this.AuthorCategories = db.AuthorCategories.ToList();
        }
    }
}

Make sure you are returning this to your view and you set your model to the AuthorViewModel class instead of the Author class:

@model YourNameSpace.AuthorViewModel

I used go one step further and instead of having a public List<AuthorCategory> AuthorCategories { get; private set; } property I would actually create as list of SelectListItem objects to make it way easier to bind to a dropdown.


Need Your Help

Windows 10 Balloon Class Name

c# c++ automation windows-10

We have a UI automation tool that waits for a Window Balloon notification to appear.

C++ Statically linked shared library

c++ static-libraries crt dynamic-library

I have a shared library used by a another application beyond my control which requires *.so objects. My library makes use of sqlite3 which needs to be statically linked with it (I absolutely need a...