Asp.net MVC,显示来自db的数据(多对多)

时间:2016-10-11 16:23:08

标签: c# asp.net-mvc razor proxy many-to-many

我有2个表,书籍和属性,它们彼此连接,与表Attribute_Book具有多对多的关系。我没有设法做创建功能,只想在详细信息视图中显示第3个表中的数据。我把manualy只是为了检查并得到一个问题,除了我要显示的值之外还显示了一个代理,它是属性名称(用断点查看).Project首先是db。谢谢你的帮助。

这是我的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; }

这是我的属性模型

    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; }

这是我的Attribute_Book模型

    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; }

这是我在控制器中的详细信息操作

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

视图Details.cshtml中我要显示数据的部分

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

详细信息视图正在使用我的ViewModel,我想也许它会有所帮助,但仍然没有和我的功能我获取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;
    }

这就是它给我看的

System.Data.Entity.DynamicProxies.Attribute_E4A8D634F0CCC98D73ED369A80817849BFA813311536941614A7242B3A2751A2 456

最后3个数字是我想得到的值))

1 个答案:

答案 0 :(得分:0)

所有问题都在考虑之中。刚刚删除了这个部分@Html.DisplayFor(model => model.Attribute_Book)并且它可以工作,我想displayfor在primarykey id之后获取了第一个数据值,这是AttributeID并试图显示它。