Home > Mobile >  Return Multiple View Model data to HTML view
Return Multiple View Model data to HTML view

Time:01-03

In my web application, there are two types of data I need to show in the html view in different tables.

So First I created two different view models to store data.

So in the controller I have wrote this code to send the data to view models

List < RecognizedPartsViewModel > Reco = new List < RecognizedPartsViewModel > ();

var rData = (from i in db.InventoryMain join p in db.PartCategory on i.PartCatogary equals p.Id where i.ReOrderQty != 0 && i.AvaQty <= i.ReOrderQty && i.PartCatogary != 0 select new RecognizedPartsViewModel {
    PartNo = i.PartNo,
    Description = i.PartDescription,
    Model = i.PartModel,
    AvaQty = i.AvaQty,
    ReOrderQty = i.ReOrderQty,
    PartCato = i.PartCatogary,
    ABCD = i.A_B_C_D_Category

}).ToList();

List < UnRecoPartsViewModel > unReco = new List < UnRecoPartsViewModel > ();

var rUnData = (from i in db.InventoryMain where i.ReOrderQty != 0 && i.AvaQty <= i.ReOrderQty && i.PartCatogary == 0 select new UnRecoPartsViewModel {
    PartNo = i.PartNo,
    Description = i.PartDescription,
    Model = i.PartModel,
    AvaQty = i.AvaQty,
    ReOrderQty = i.ReOrderQty,
    ABCD = i.A_B_C_D_Category

}).ToList();

So I need to know that this data I have to show in the same html view. So how can I call these two view model as separate list in the html view?

CodePudding user response:

If your data model has structure described in the question you can use the one class for two cases:

public class PartsViewModel
{
    public string PartNo { get; set; }
    public string Description { get; set; }
    public string Model { get; set; }
    public int AvaQty { get; set; }
    public int ReOrderQty { get; set; }
    public string PartCato { get; set; } // Used only in the a table for RecognizedParts
    public string ABCD { get; set; }
}

And then create a new class to included separated list for each category:

public class PartsDataModel 
{
    public List<PartsViewModel> RecognizedParts { get; set; }
    public List<PartsViewModel> UnRecognizedParts { get; set; }
}

Then:

public ActionResult Index()
{      
   // ... copy your previous code here, but use `PartsDataModel` class 
   // instead of `RecognizedPartsViewModel `UnRecoPartsViewModel`. 
    
   var model = new PartsDataModel() 
   {
       RecognizedParts = rData,
       UnRecognizedParts = rUnData
   }  
   
   // Create the view
   return View(model);
}

CodePudding user response:

You need to create another model that can take both types of data, that is 1 List < RecognizedPartsViewModel > and 1 List < UnRecoPartsViewModel > and you would pass this model to your view and access each one of your lists using this model

  • Related