Douglas
Douglas

Reputation: 177

Calling two stored procedures and combining results into a single model within one controller

I am trying to call multiple stored procedures from one controller in my MVC5 web application. I then combine the results of the two procedures into one model.

public ActionResult ItemDetails(string itemNo, string custNo)
{
    SqlParameter[] param =
   {
     new SqlParameter("@itemNo",itemNo),
     new SqlParameter("@custNo", custNo)
    };

    var sqlData = itemDb.Database.SqlQuery<ItemDetailsModel>("ItemDetail @itemNo, @custNo", param).ToList();

    var whsID = new SqlParameter("@warehouse", sqlData?[0].WhsID);

    var sqlData2 = itemDb.Database.SqlQuery<ItemDetailsModel>("ItemPoLookup @itemNo, @warehouse", new SqlParameter("@itemNo", itemNo), whsID).ToList();

    sqlData.AddRange(sqlData2);

    return View(sqlData);
}

Is it possible to make this happen while keeping the models separate? Should I do two get requests to my web server, one for each stored procedure, instead of combining this all into one? And if so advice on returning multiple models to the cshtml view would be appreciated.

Upvotes: 0

Views: 1483

Answers (2)

codeandcloud
codeandcloud

Reputation: 55333

Pass it like

List<List<<ItemDetailsModel>>

or

Dictionary<string, List<ItemDetailsModel>>`

If you are using Dictionary<TKey, TValue> you can pass a string to identify the two stored procedures.

Upvotes: 1

Ja9ad335h
Ja9ad335h

Reputation: 5075

if you dont want to create an other ViewModel and want the results seperate, use this (you may not get intellisense while using this model in view)

dynamic viewModel = new ExpandoObject();
viewModel.sqlData = sqlData;
viewModel.sqlData2 = sqlData2;

return View(viewModel);

Upvotes: 1

Related Questions