0

I have this code that works.

public class HelloWorldController : Controller
{
    UAStagingEntities db = new UAStagingEntities();

    public ActionResult Index(int? id)
    {
        var depot = db.CSLA_DEPOT.Where(c => c.DEPOT_ID == id.Value);

        return View(depot.ToList());
     }

}

What I don't know how to do is display a view with results from two queries. How would I create the view to show both depot and address? and how would I code the return statement?

public class HelloWorldController : Controller
{
    UAStagingEntities db = new UAStagingEntities();

    public ActionResult Index(int? id)
    {
         var depot = db.CSLA_DEPOT.Where(c => c.DEPOT_ID == id.Value);
         var Address = db.CSLA_ADDRESS.Where(a => a.CSLA_DEPOT.DEPOT_ID == id.Value);

         return View(depot.ToList());
    }
}

EDIT *

I added this model

    namespace CustomerCareMVC.Models
{
    public class CSLA_StagingModel

    {

        public List<CSLA_DEPOT> depots { get; set; }
        public List<CSLA_ADDRESS> addresses { get; set; }

    }

}

And added this method in the controller

    public ActionResult ShowAllTables() 
    {


        var model = new CSLA_StagingModel()
        {

            depots = db.CSLA_DEPOT.Where(c => c.DEPOT_ID == 10065),
            addresses = db.CSLA_ADDRESS.Where(a => a.CSLA_DEPOT.DEPOT_ID == 10065),
        };


        return View(model);

    }

I get squiggly line under these two lines

            depots = db.CSLA_DEPOT.Where(c => c.DEPOT_ID == 10065),
            addresses = db.CSLA_ADDRESS.Where(a => a.CSLA_DEPOT.DEPOT_ID == 10065),

with this error message

Error 1 Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?) C:\CustCareMVC\CustomerCareMVC\CustomerCareMVC\Controllers\HelloWorldController.cs 59 26 CustomerCareMVC

2 Answers 2

2

You would need to create a ViewModel that has the following paramters:

public IQueryable<CSLA_DEPOT> depots {get; set;}
public IQueryable<CSLA_ADDRESS> addresses {get; set;}

You would then need to create an instance of this new View Model in your controller like so:

var model = new ViewModelName(){
    depots = db.CSLA_DEPOT.Where(c => c.DEPOT_ID == id.Value),
    addresses = db.CSLA_ADDRESS.Where(a => a.CSLA_DEPOT.DEPOT_ID == id.Value),
};

You would then need to pass this model to your view like so:

return View(model);

In your view you would access the two different collections like so:

Model.depots
Model.addresses

Hope this helps, leave me a comment if you have any questions.

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

2 Comments

DevDave, I'm trying to implement this code and I'm getting an error. Added it to the question under the work EDIT. thanks
Joshua, I had a bug in my code. Please note that the model is now referencing IQueryable for each variable instead of list. This should solve your problem.
0

Create a special object that contains two items of the types returned by your queries. Then you can access them with Model.depot and Model.Address in your View.

3 Comments

do you not know the types returned by the queries?
No i don't. I'm querying tables and filtering they by the id number which is entered by the user on the webpage.
in your models folder. you could create a class that contains two fields. one for each list. i think you could use the types created by Entity Framework. then for the strongly typed view, select the class created with the two lists.

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.