tags:

views:

34

answers:

1

I want to return the results of a query that is written in linq to my view page for a MVC application.

I want to call my view, then pass the model, the model would have the results of my query.

How can I do this?

+2  A: 
public ActionResult YourAction() {
  var results = GetResultsFromLinqQuery();
  return View(results);
}

public List<SomeObject> GetResultsFromLinqQuery() {
   var queryResults = // get these from your linq query
   return queryResults;
}

If you strongly type your view to expect results, then you can access them using Model.

Edit:

If your results is a List then in the view replace

Inherits="System.Web.Mvc.ViewPage"

with

Inherits="System.Web.Mvc.ViewPage<IEnumerable<SomeObject>>"

Edit 2:

If all of this is not making much sense to you, I suggest you to take a look at Nerd Dinner tutorial for ASP.NET MVC.

Mahesh Velaga
how can i add my result if i want it to be strongly typed?
WingMan20-10
updated the answer
Mahesh Velaga
what would be the return type of GetResultsFromLinqQuery(); return a list?can you show me how you would declare GetResultsFromLinqQuery(); to return the poper list
WingMan20-10
updated again !
Mahesh Velaga
I just want to know how i should declare GetResultsFromLinqQuery();
WingMan20-10
OK, hopefully that will give some help then, do accept posts as answers if they helped to solve your problem. Thanks
Mahesh Velaga