0

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?

1 Answer 1

2
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.

Sign up to request clarification or add additional context in comments.

4 Comments

how can i add my result if i want it to be strongly typed?
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
I just want to know how i should declare GetResultsFromLinqQuery();
OK, hopefully that will give some help then, do accept posts as answers if they helped to solve your problem. Thanks

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.