0

I have a model which is storing mycustomer new request information. In another history model i am storing all previous request of the customer. In view i would like to take new order and also see his previous orders and suggest some food after seeing his previous order.

Here are my models...

    public class CustomerFoodModel
    {
    public DateTime FoodRequestCreated { get; set; }
    public string FoodRequestType { get; set; }
    ...
    ...
    }

    public class CustomerHistoryModel
    {
    public string Name { get; set; }
    public DateTime FoodRequestCreated { get; set; }
    public string FoodRequestType { get; set; }
    ...
    ...
    }

Helper.cs file

    public static CustomerFoodModel getCustomerDetails(int id) // id is loyalty card number
     {
        // get details from (cutomer) sql table

        //store it in (CustomerFoodModel)

        // check if it has previous orders
         getCustomerHistoryDetails(id);
        ....


     }

     public static CustomerHistoryModel getCustomerHistoryDetails(int id)
     {
     // get deails from (history) sql table
     // store it in (CustomerHistoryModel
     }

In my controller, I am passing my (CustomerFoodModel) to the view.

      public ActionResult EditCustomerRequest(int id, string name, string date)
    {
        CustomerFoodModel CRequest =  Helper.getCustomerDetails(id);
        ...

        return PartialView("EditCustomerRequest",CRequest);

        }

How do I show the (CustomerHistoryModel) in the same view.? Is there possible to include (CustomerHistoryModel) in (CustomerFoodModel)?

user1651888
  • 463
  • 2
  • 9
  • 25

4 Answers4

6

Create a new class to wrap both of the model.

public class CustomerFoodModel
{
   public CustomerFoodModel CustomerFood { get; set; }
   public CustomerHistoryModel CustomerHistory { get; set; }
}

And on your controller

public ActionResult EditCustomerRequest(int id, string name, string date)
{
    CustomerFoodModel CRequest =  Helper.getCustomerDetails(id);
    CustomerHistoryModel CHModel = Helper. getCustomerHistoryDetails(id);
    return PartialView("EditCustomerRequest",new CustomerFoodModel(){
         CustomerFood = CRequest,
         CustomerHistory = CHModel
    });

}
Martin Valentino
  • 1,002
  • 2
  • 11
  • 26
3

Use wrapper class which contain both of class

public class CustomerViewModel
{
    public CustomerFoodModel FoodModel { get; set; }
    public CustomerHistoryModel HistoryModel { get; set; }
}
stackada
  • 414
  • 1
  • 6
  • 16
3

I think the best approach is to use a partial view inside the main view. The partial view can call back to another controller to get a new model and pass that model to the partial view. This keeps things better seperated.

Look at this post for a similar issue. Using partial views in ASP.net MVC 4

Community
  • 1
  • 1
ToddB
  • 1,464
  • 1
  • 12
  • 27
1

You have a few options. I would probably could create a view model that contains both of your models:

public class CustomerViewModel
{
    public CustomerFoodModel FoodModel { get; set; }
    public CustomerHistoryModel HistoryModel { get; set; }
}

Or, depending on your data structure, you may have multiple history entries per customer:

public class CustomerViewModel
{
    public CustomerFoodModel FoodModel { get; set; }
    public List<CustomerHistoryModel> HistoryModels { get; set; }
}

Then your getCustomerDetails function would return a CustomerViewModel instead.

AJ Richardson
  • 6,610
  • 1
  • 49
  • 59