0

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 ))

Hovo
  • 93
  • 1
  • 1
  • 7
  • If you don't specify any configuration for the relation, entity framework generates Proxies for your relation – Zinov Oct 11 '16 at 16:37
  • Sorry but i want to know , if i have all the foreign keys and all this is just a result of scafolding , i must add what and where for project to work correctly? – Hovo Oct 11 '16 at 17:12
  • https://msdn.microsoft.com/en-us/data/jj592886.aspx and see this reference too http://stackoverflow.com/questions/7111109/should-i-enable-or-disable-dynamic-proxies-with-entity-framework-4-1-and-mvc3. If that help you let me know to put it as an answer – Zinov Oct 11 '16 at 17:30
  • i read both topics but the second one is to hard for me to understand , i did as described in msdn but it throws such error "Object reference does not point to an object instance." . i suppose its becouse of disabling proxy in dbcontext constructor , its not recognizing no virtual properties if im right. – Hovo Oct 12 '16 at 06:30

1 Answers1

0

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.

Hovo
  • 93
  • 1
  • 1
  • 7