Fraser
Fraser

Reputation: 14246

Passing the results from multiple stored procedures to a view

.net virgin here so please excuse any blinding errors on my part.

In my application, the majority of data lifting is done with SQL Server stored procedures.

From my controller, I am able to pass the results of the store procedure like so:

var vehiModels = db.spVehicleGetModels(ID_make, false, true, ID_country);
return View(vehiModels.ToList());

However, I want to be able to pass the results of multiple stored procedures to my view. If I wasn't using stored procedures this could be done with viewmodels but I'm struggling to find a way of doing this with my stored procedures.

Does anyone have any insight or a link to a good explanation? Google has not brought up much help.

Cheers

Upvotes: 1

Views: 2059

Answers (3)

JotaBe
JotaBe

Reputation: 39025

Apart form very simple CRUD applications, the ViewModel should be a custom class which have the data needed by the view engine to render the HTML page for the browser.

That said, you need to create an adequate ViewModel for your case. The problem is that you later need to feed that model (i.e. set the property values).

One option is to keep the coupling between your data layer and your view model, and create a view model whose properties are exactly of the type returned by the data layer. (I don't like this kind of coupling).

Another option is to create exactly what I said above: a ViewModel class created exactly for the view needs. In this case you need to use soemthing like Automapper or Value injecter to copy the data returned from the query to the ViewModel. Any of these solutions is able to copy data from a source (in this case your stored proc results) to a destination (in this case, the viewmodel). Both work with a combination of conventions/configuration. They're quite easy to use.

And you can also do it by hand, using LINQ. You can map, and shape, the results from its original form to a destination class using the projection funcion Select, for example, like this:

StoredProdResult.Select(spr => new DestinationClass {
  DestinationProperty1 = spr.OriginalColumn1,
  DestinationProperty2 = spr.OriginalColumn4,
  DestinationProperty3 = spr.OriginalColumn2 + " " + spr.OriginalColumn3
 }).ToList()

Upvotes: 0

Dustin Kingen
Dustin Kingen

Reputation: 21245

A ViewModel simply holds all the data a view needs. Make a class to hold the vehicle collection and any other data the view needs and pass it to the page. Then you can strongly type the page with the ViewModel.

public class VehicleViewModel
{
    public ICollection<VehicleModel> VehicleModels { get; set; }
}

public ActionResult Vehicles(int? makeId, int? countryId)
{
    if(!makeId.HasValue || !countryId.HasValue)
    {
        RedirectToAction("Error");
    }

    var models = db.spVehicleGetModels(makeId, false, true, countryId);
    var viewModel = new VehicleViewModel { VehicleModels = models.ToList() };
    return View(viewModel);
}

Upvotes: 2

Masoud
Masoud

Reputation: 1351

Two way could work in this situation:
frist you can create a super model and make each stored proces to an element of that model! Like this:
How to edit multiple models in a single Razor View
Or you can use tuples in your view:
http://www.dotnetperls.com/tuple
http://msdn.microsoft.com/en-us/library/system.tuple%28v=vs.110%29.aspx

Upvotes: 1

Related Questions