我一直在寻找这个问题了大约两个星期,现在并认为这是一次我寻求帮助......MVC音乐商店链接表数据
我试图让MVCMusicStore购物车教程工作第8部分:http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-8。我的项目的主要区别在于,我使用的是数据库优先(而不是Code First,用于MVC实践/托管数据库)。
类代码:
public partial class Cart
{
public int RecordId { get; set; }
public string CartId { get; set; }
public int AlbumId { get; set; }
public Nullable<int> Count { get; set; }
public Nullable<System.DateTime> DateCreated { get; set; }
public virtual Album Album { get; set; }
}
public partial class Album
{
public int AlbumId { get; set; }
public Nullable<int> GenreId { get; set; }
public Nullable<int> ArtistId { get; set; }
public string Title { get; set; }
public Nullable<decimal> Price { get; set; }
public string AlbumArtUrl { get; set; }
public virtual Genre Genre { get; set; }
public virtual Artist Artist { get; set; }
}
public class ShoppingCartViewModel
{
public List<Cart> CartItems { get; set; }
public decimal CartTotal { get; set; }
}
函数来填充CartItems在ShoppingCartViewModel:
public List<Cart> GetCartItems()
{
return db.Carts.Where(cart => cart.CartId == ShoppingCartId).ToList();
}
.cshtml页:
@model BoothPimps.ViewModels.ShoppingCartViewModel
@foreach (var item in Model.CartItems)
{
<tr id="[email protected]">
<td>
@Html.ActionLink(item.Album.Title, "Details", "Store", new { id = item.AlbumId }, null)
</td>
</tr>
}
这里是Model.CartItems的图像,填充除连结相簿资料外的所有内容: http://s1253.photobucket.com/albums/hh585/codingcoding1/?action=view¤t=image1.jpg (项目名称去掉无论scribbed)
代码中的错误的位置:
@Html.ActionLink(item.Album.Title, "Details", "Store", new { id = item.AlbumId }, null)
问题:item.Album总是空。
Album.AlbumId = Cart.AlbumId应该将相册数据链接到购物车,以便它不会返回null,但它不起作用。在前面的教程但是当我做同样的事情,但是从专辑获得流派或艺术家的数据链接的数据工程,我能够检索值,就像这样:
@Html.DisplayFor(model => model.Genre.Name)
@Html.DisplayFor(model => model.Artist.Name)
那么怎么来model.Genre艺术家返回值,但item.Album为空?我是不是使用“虚拟”关键字以相同的方式链接这些值?我错过了什么?
感谢您看看这个。
为您截图并将其添加到帖子中。您也可以使用此链接:http://s1253.photobucket.com/albums/hh585/codingcoding1/?action=view¤t=image1.jpg – ForeverLearningAndCoding 2012-07-18 02:09:19