2009-07-28 72 views
1

我創建了一個自定義實體創建一個強類型MVC視圖,因爲我需要填充的實體從L2S一個加入了一些數據。如何基於自定義LINQ2SQL類

當我用鼠標右鍵單擊在控制器中的「添加視圖」 ActionResult的代碼,然後選擇「創建強類型視圖」,我的課並不在選擇可用的類顯示出來。我不知道爲什麼。這裏是我的代碼:

//The Model 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Data; 
using System.Data.SqlClient; 

namespace FurnitureStore.Models.Repository 
{ 
    public class FurnitureRepository 
    { 
     public IQueryable<Listing> GetProductListings() 
     { 
      FurnitureDataContext dc = new FurnitureDataContext(); 

     return (from p in dc.Products 
       join c in dc.Categories 
       on p.CategoryId equals c.CategoryId 
       select new Listing 
       { 
        ProductName = p.ProductName, 
        CategoryDescription = c.CategoryDescription 
       }); 
    } 
} 
} 

//The entity class 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace FurnitureStore.Models 
{ 
    public class Listing 
    { 
     public string ProductName { get; set; } 
     public string CategoryDescription { get; set; } 
    } 
} 

//The Method in the Controller 
public ActionResult ProductListings() 
{ 
    FurnitureRepository fr = new FurnitureRepository(); 
    var listings = fr.GetProductListings(); 
    return View("ProductListings",listings); 
} 

回答

1

只需創建一個普通視圖並編輯視圖的頁面定義(特別是繼承屬性)自己。

<%@ Page ... Inherits="System.Web.Mvc.ViewPage<IQueryable<FurnitureStore.Models.Listing>>" %> 

關閉我的頭頂我無法回答爲什麼它沒有出現在您的類選擇器中。

HTHS
查爾斯

3

請確保您編譯代碼,如果代碼不編譯新加入的類不showup在選擇可用的類

+1

這是解決絕對正確的方法這個! – 2011-04-05 09:09:21

相關問題