2012-09-29 22 views
0

我有一個詳細信息頁面,返回配置文件視圖,基本上用於從數據庫中查找頁碼。我也有一個單獨的查詢,我想返回返回視圖但無法弄清楚如何。我的代碼是mvc 3與其他對象的返回視圖

public ActionResult detail(int id) 
    { 
     profile profile = db.profiles.Find(id); 
     var currentpage = (from s in db.profiles where id == s.profileID select s.registrationID).FirstOrDefault(); 
     var articles = (from s in db.Articles where currentpage == s.RegistrationID select s.title).FirstOrDefault(); 
     ViewBag.articles = articles; 


     ViewBag.noprofiler = " This profile currently doesn't have the email profiler selected"; 




     return View(profile); 
    } 

我想也返回文章我把它變成一個viewbag,但不工作的任何想法?

+0

文章應該可用,如果您將其添加到ViewBag,只要它被分配在控制器中的值。您可能在視圖中使用的代碼訪問ViewBag中的文章時出現問題。你能否通過你曾經使用過的視圖代碼來做到這一點? –

回答

0

如果物品填充,那麼你應該能夠通過ViewBag.articles

訪問它的觀點也許你應該檢查什麼是在控制器功能變量這裏的文章。

此外,變量名稱文章表明您正在尋找一個列表,但您正在使用FirstOrDefault(),它將返回單個對象(或null)。

+0

哦,好吧,但對於我來說,我將不得不使用.tolist方法,然後我需要使我的模型具有可枚舉性,但是這給了我一個錯誤,因爲「profile」的返回是單數 – user1591668

+0

你不會需要使模型成爲IEnumerable。您可以將任何東西放入ViewBag中,並且仍然有一個配置文件作爲模型。因此,您可以使用.ToList()並將列表放入ViewBag.articles中,然後使用視圖中的文章列表。 –

相關問題