2009-07-28 66 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

相关问题