I am working on a web based penny auction portal. I am using PetaPoco as my data access class library with Asp.net MVC 3 architecture. I am facing an issue with multi-table data pull using stored procedure. I have created a view model POCO for mapping stored procedure fields, that I need on the front end.
View model:
public class BiddersViewModel
{
public Guid UserId { get; set; }
public String UserName { get; set; }
public DateTime LastActivityDate { get; set; }
public int NumberOfBids { get; set; }
public int AuctionId { get; set; }
public int BidId { get; set; }
public decimal BidAmount { get; set; }
}
Controller
public ActionResult Index()
{
var context = new PetaPoco.Database("DataContext");
return View(context.Query<dynamic>("exec udsp_Bidders_SelectAll"));
}
What I need
I need to pull the data from the database with multiple joins and display on the view using stored procedure (using my view model class).
If your stored procedure returns the same column names as you use in your POCO class, you can change your code to:
and your view should work as expected, because you’re likely having a strong type view with defined model type as
Ienumerable<BiddersViewModel>.Don’t worry in case your stored procedure returns more columns that you defined within your POCO view model class. Just make sure that those that you wan’t mapped have correct names (or use
ColumnAttributeto map them to properties).Regarding
Fetch/Querydifference is just that if you useFetchyou will be reading records in your controller action, but if you useQueryand pass that to your view, you will be reading records in the view. It’s usually a better practice to prepare the data in controller action and use that data in the view. To support this pattern you should either useFetchorQuery<T>().ToList()in your controller action.But if you need to manipulate those results for some reason in the view (if it can’t be done any other way) then use
Queryand pass actual enumerator to view which will then manipulate results and read them accordingly.