Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Returning Multiple Variables to View

I am having trouble figuring out how to return multiple variables to a view. Something like this. Can I get a little help?

    public ActionResult CheatSheet()
    {
        var var1 = from ts in db.thisdatabase
                   select ts;

        var var2 = from ls in db.thisdatabase
                   select ls;

        var var3 = from d in db.thisdatabase
                   select d;

        return View(var1,var2,var3);
    }
like image 576
Alex D Avatar asked Mar 10 '23 04:03

Alex D


1 Answers

Consider Using a ViewModel

You'll want to use a ViewModel to compose all of these different results and pass that model to the View:

public class ExampleViewModel
{
      // Example collections for each of your types
      public IEnumerable<Something> CollectionA { get; set; }
      public IEnumerable<Something> CollectionB { get; set; }
      public IEnumerable<Something> CollectionC { get; set; }
}

And then just use something like the following code to perform your specific queries and use the results of those queries to build your model and then pass it along to the View:

// Build the model
var model = new ExampleViewModel(){

     // You'll likely want a .ToList() after these to ensure things work as expected
     CollectionA = db.thisdatabase.Select(x => x.ts),
     CollectionB = db.thisdatabase.Select(x => x.ts),
     CollectionC = db.thisdatabase.Select(x => x.ts),
};

// Pass it to your View
return View(model);

Note: This assumes that you aren't actually querying the same exact table with each of your queries. If that was the case, then it may be more efficient to pull back a single collection with each of your properties and then assign your individual collections of properties to the model (instead of performing multiple, possibly redundant, queries.

Then within the View, you can reference the underlying property collections as expected and iterate through them or perform any other types of operations:

@model YourProject.ViewModels.ExampleViewModel

@foreach (var item in Model.CollectionA) { ... }
@foreach (var item in Model.CollectionB) { ... }
@foreach (var item in Model.CollectionC) { ... }

For More Complex Scenarios

If you didn't want to simply access a single column from your database but rather multiple, you'll likely want to create another model/class to map your properties and then store instances of those within your ViewModel.

Let's look at your example and see how that might work. So you currently are looking to store the ts, ls and d properties, so let's make a class to store those in:

public class Example
{
     public string Ts { get; set; }
     public string Ls { get; set; }
     public string D { get; set; }
}

Now, when you perform your query, simply grab all of these and map them within a Select() call:

// This will now be an IEnumerable<Example>
var models = db.thisdatabase.Select(x => new Example()
{
    Ts = x.ts,
    Ls = x.ls,
    D = d
});

You could now pass this along directly to your View if that's all that you needed:

// If you did this, you'd need to adjust your @model declaration
return View(model);

Or you could perform multiple of these if you needed to build different models for different tables and then compose all of those collections into a ViewModel similar to the original example:

var model = new ExampleViewModel(){
     CollectionA = db.thisdatabase.Select(x => new Example(x)),
     CollectionB = db.othertable.Select(x => new OtherExample(x)),
     CollectionC = db.yetanother.Select(x => new LastExample(x))
};

Other Forms Of Storage

There are a few other approaches you could consider depending on your needs, such as using the ViewBag collection. The ViewBag is a dynamic collection that allows you to easily store and access objects within the View:

ViewBag.A = db.thisdatabase.Select(x => x.ts),
ViewBag.B = db.thisdatabase.Select(x => x.ts),
ViewBag.C = db.thisdatabase.Select(x => x.ts),

return View();

This will essentially work the same way, but instead of references @Model within your View, you would just use @ViewBag instead. It's also worth noting that this can also be used in conjunction with an actual model as well.

There are other approaches such as using the ViewData or Session collections, but you really should use a model. It's more in line with the actual MVC pattern, and if you get used to it, it should make your life much easier.

like image 95
Rion Williams Avatar answered Mar 12 '23 06:03

Rion Williams