2012-09-20 127 views
0
public ViewResult Details(int ID) 
    {   
     tblMp3 item = db.tblMp3.Single(t => t.ID == ID); 
     ViewBag.Mp3Tags= db.tblTags.Where(c => c.tblMp3.Any(a => a.ID == ID)).ToList(); 
     return View(item); 
    } 

這是我的控制器,我讀到它不推薦使用ViewBag,我必須使用Model,但不幸的是我不知道該怎麼做,任何人都可以幫忙?由於將ViewBag轉換爲模型

回答

0

只要創建新類這樣的:

public class Mp3ViewModel 
{ 
    public tblMp3 Item { get;set; } 
    public SomeClass Mp3Tags { get;set; } 
} 

併發送它來查看。

鑑於你使用:

@model namespace.Mp3ViewModel 

現在你可以使用Model.ItemModel.Mp3Tags

2

在您查看,添加該在第一行:

@model YouNamespace.tblMp3 

後,您可以使用您的模型的屬性:

<p>@Model.Title</p>