我一直在尋找這個問題了大約兩個星期,現在並認爲這是一次我尋求幫助......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