Search code examples
c#asp.net-mvcrazorproxymany-to-many

Asp.net MVC , showing data from db(many to many)


I have 2 tables , Books and Attributes , they are connectied to each other with many to many relationship with the table Attribute_Book . I didnt managed to do the create function , just want to show the data from the 3rd table in details view. which i put manualy just to check and getting a problem , beside the value which i want to show it's showing also a proxy which is the attribute name (saw it with breakpoint).Project is db first. Thanks for any help.

this is my Books ViewModel

    public BookModel()
    {
        this.Attribute_Book = new HashSet<Attribute_Book>();
    }

    public int BookID { get; set; }

    public string Title { get; set; }

    public int AuthorID { get; set; }

    public int CountryID { get; set; }

    public decimal Price { get; set; }

    public string Description { get; set; }

    public Nullable<int> PagesCount { get; set; }

    public string Picture { get; set; }

    public decimal TotalPrice { get; set; }

    public virtual ICollection<Attribute_Book> Attribute_Book { get; set; }
    public virtual Author Author { get; set; }
    public virtual Country Country { get; set; }

This is my Attribute Model

    public Attribute()
    {
        this.Attribute_Book = new HashSet<Attribute_Book>();
    }

    public int AttributeID { get; set; }
    public string Name { get; set; }
    public int AttTypeID { get; set; }

    public virtual ICollection<Attribute_Book> Attribute_Book { get; set; }
    public virtual Attribute_Type Attribute_Type { get; set; }

And this is my Attribute_Book model

    public int ID { get; set; }
    public int BookID { get; set; }
    public int AttributeID { get; set; }
    public string ValueTypeText { get; set; }
    public Nullable<System.DateTime> ValueTypeDate { get; set; }

    public virtual Attribute Attribute { get; set; }
    public virtual Book Book { get; set; }

This is my Details Action in Controller

    public async Task<ActionResult> Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        //Book book = await db.Books.Include(b=>b.Attribute_Book).FirstOrDefaultAsync(b=>b.BookID==id);

        Book book = await db.Books.FindAsync(id);

        BookModel model = GetBooks.Details(book);

        if (book == null)
        {
            return HttpNotFound();
        }
        return View(model);
    }

And the part of the view Details.cshtml where i want to show data

    <dd>
        @Html.DisplayFor(model => model.Attribute_Book)
        @{
            var extAtt = Model.Attribute_Book.FirstOrDefault();
            if (extAtt != null)
            {
                @extAtt.ValueTypeText
            }
        }
    </dd>

details view is using my ViewModel , i thought maybe it will help , but still not and my function where i get details for viewmodel

public static BookModel Details(Book item)
    {
        var model = new BookModel
        {
            BookID = item.BookID,
            Title = item.Title,
            PagesCount = Convert.ToInt32(item.PagesCount),
            Description = item.Description,
            Price = item.Price,
            CountryID = item.CountryID,
            AuthorID = item.AuthorID,
            Author = item.Author,
            Country = item.Country,
            Picture = item.Picture,
            TotalPrice = item.Country.TelCode + item.Price,
            Attribute_Book = item.Attribute_Book.ToList()
        };
        return model;
    }

this what it shows me

System.Data.Entity.DynamicProxies.Attribute_E4A8D634F0CCC98D73ED369A80817849BFA813311536941614A7242B3A2751A2 456

last 3 numbers is the value which i want to get ))


Solution

  • All the problem was in view . Just removed this part @Html.DisplayFor(model => model.Attribute_Book) and it works , i suppose displayfor is taking first value of data after primarykey id which was AttributeID and trying to show it.