1

I created one model in my ASP.net MVC Project :

public class ProductListingModels:ItemEntityDataContext
{
  public int ID { get; set; }
  public string Name { get; set; }
  public int DepartmentID { get; set; }
  public int BrandID { get; set; }
 }

And I have one controller :

public class ProductListingController : Controller
{
   // GET: /ProductListing/
   public JsonResult Index(string depID)
   {
   Context DataContext = new Context();
   JsonResult jr = new JsonResult();
   int dep = Convert.ToInt32(depID);
   var ien_item =   from i in DataContext.DataContext.Items
        join c in DataContext.DataContext.Categories on i.CategoryID equals c.ID
        join d in DataContext.DataContext.Departments on i.DepartmentID equals d.ID
        join brand in DataContext.DataContext.Brands on i.BrandID equals brand.ID
        orderby i.LastUpdated descending
        where i.DepartmentID == dep && i.Active > 0 && i.WebsiteShow > 0 && c.Active > 0 

                    select i;

     List<ProductListingModels> prom = new List<ProductListingModels>();
     //
     //Adding ien_item to the prom
     //
     jr.Data = prom;
         jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
         return jr;
   }
}
public class Context : ICEWeb.Models.ItemEntityDataContext
{

}

I want to add each data that I have query from database by linq (ien_item) to the object of ProductListingModel(prom object), and then return it as the json to my view.

Could anyone give me some ideas please.

Thanks so much.

1 Answer 1

1

First, your model shouldn't be inheriting from the data context. Passing your entire context to the view seriously violates the separation of concerns that is fundamental to MVC. Second, you should simply select into elements of the view type, then call ToList() on the result.

var model = (from i in DataContext.DataContext.Items
             join c in DataContext.DataContext.Categories on i.CategoryID equals c.ID
             join d in DataContext.DataContext.Departments on i.DepartmentID equals d.ID
             join brand in DataContext.DataContext.Brands on i.BrandID equals brand.ID
             orderby i.LastUpdated descending
             where i.DepartmentID == dep && i.Active > 0 && i.WebsiteShow > 0 && c.Active > 0 
             select new ProductListingModels
             {
                 ID = i.ID,
                 Name = i.Name,
                 ...
             }).ToList();

Then simply return the data using the Json() convenience method rather than constructing your own response.

 return Json( model, JsonRequestBehavior.AllowGet );
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks tvanfosson, but if I have more than ten field I want to select, do I have to use SELECT NEW {......} ?
You could also use something like AutoMapper, automapper.org, to map between your entities and your view models. In that case you'd select just the item and call ToList() to get them in memory, then (assuming you have your mapping set up) do, .Select( m => Mapper.Map<Items,ProductListModels>() );
If the object describes a product then the class name of the model should be Product, not ProductListingModels. You should find and follow some of the very good MVC examples to learn how things should be done and how they should be laid out. Your code sample shows a lack of understanding in several areas.
@JK. I wasn't going to quibble. Drop the pluralization and I'd be ok with it, ProductListingModel seems to describe it. I'm a little more concerned with a table of Items -- that could be anything.
@JK : Thanks JK, yes sure. I am a pretty developer about asp.net mvc, that's why I am very poor of it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.