Dkong
Dkong

Reputation: 2788

How to create a strongly typed MVC View based on a custom Linq2Sql class

I have created a custom entity because I need to populate an entity with some data from a join in L2S.

When I right click on the ActionResult code in the Controller to "Add View", and then choose "Create strongly typed view", my class doesn't show up in the classes available in the selector. I'm not sure why. Here is my code:

//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);
}

Upvotes: 1

Views: 622

Answers (2)

San
San

Reputation: 2346

Make sure that you compile the code, if the code is not compiled the newly added classes dont showup in the classes available in the selector

Upvotes: 3

ten5peed
ten5peed

Reputation: 15890

Just create a normal view and edit the view's page definition (inherits attribute specifically) yourself.

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

Off the top of my head I can't answer why it isn't appearing in your class selector.

HTHs
Charles

Upvotes: 1

Related Questions