sspn
sspn

Reputation: 31

Asp.net mvc4 and Entity framework, display Name instead of displaying Id for two related tables

I have two tables, Book and Category.

-------------               --------------
|   book    |               |   Category  |
-------------                --------------
|Id         |               | Id          |
|CategoryId |               | CategoryName|
|Title      |               ---------------
|Description|
------------

I used entity framework to get data from database and return to view as something like this:

Controller

using (var db = new BookEntities()){
     return View(db.Book.ToList())
} 

I can display all data in book table but i don't know how to display Category name instead of displaying CategoryId.

I am new to Asp.net mvc4 and entity framework.

Please help me. Thanks

Upvotes: 1

Views: 5724

Answers (4)

Er. Binod Mehta
Er. Binod Mehta

Reputation: 63

View(.Cshtml)

<tr>
<td>@TradingHR.Repository.Repo.GetTeamMemberName(item.IdMember)</td>
</tr>

--In Repo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Web;


namespace TradingHR.Repository
{
public class Repo {

public static string GetTeamMemberName(long idMemeber)
            {
                DBEntities db = new DBEntities();
                var value = (from c in db.Mi_TeamMember
                             where c.Id == idMemeber
                             select c.Name.FirstOrDefault();
                return value;
            }
}
}

Upvotes: 0

Idrees Khan
Idrees Khan

Reputation: 7752

First create a view model

public class BookViewModel
{
    public int id;
    public int Title;
    public int Description;
    public Category Category;
}

public class Category
{
    public int CategoryId;
    public string CategoryName;
}

In your linq query try this;

public List<BookViewModel> GetList()
{
    var books = db.Book
               .select(b=> new BookViewModel()
               {
                   id = b.id,
                   Title = b.Title,
                   Description = b.Description,
                   Category = b.Category
               });
       return books.ToList<BookViewModel>();
}

Now, in your view, right at the top of the page as;

@model IEnumerable<BookViewModel>

and then you can access it like;

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

Upvotes: 1

Trey Gramann
Trey Gramann

Reputation: 2004

Did you start with the database tables first and then let EF create your model? If so, there must not be a Foreign Key relationship in the database between the two tables.

Otherwise this should work perfectly as your view:

@using NamespaceOfBookModel
@model IList<Book>
@foreach (var book in Model)
{
    @book.Title
    @book.Category.CategoryName
}

Upvotes: 0

Alec Sanger
Alec Sanger

Reputation: 4562

Within your model definition of Book, you can define an object that represents the related Category record like so:

public virtual Category Category{ get; set; }

Make sure the CategoryId is configured as an FK. You can then grab the CategoryName with Book.Category.CategoryName

Upvotes: 1

Related Questions