如何在同一个MVC视图中显示两个模型

时间:2015-05-16 16:25:17

标签: c# asp.net-mvc asp.net-mvc-4

我有一个存储mycustomer新请求信息的模型。 在另一个历史模型中,我存储了客户的所有先前请求。 在我看来,我想接受新的订单,并看到他以前的订单,并在看到他以前的订单后提出一些食物。

以下是我的模特......

    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文件

    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
     }

在我的控制器中,我将我的(CustomerFoodModel)传递给视图。

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

        return PartialView("EditCustomerRequest",CRequest);

        }

如何在同一视图中显示(CustomerHistoryModel)。是否可以在(CustomerFoodModel)中包含(CustomerHistoryModel)?

4 个答案:

答案 0 :(得分:6)

创建一个新类来包装模型。

public int CompareTo(Natural x) { return CompareTo(this, x); }
public static bool operator <(Natural x, Natural y) { return CompareTo(x, y) < 0; }
public static bool operator >(Natural x, Natural y) { return CompareTo(x, y) > 0; }
public static bool operator <=(Natural x, Natural y) { return CompareTo(x, y) <= 0; }
public static bool operator >=(Natural x, Natural y) { return CompareTo(x, y) >= 0; }
public static bool operator ==(Natural x, Natural y) { return CompareTo(x, y) == 0; }
public static bool operator !=(Natural x, Natural y) { return CompareTo(x, y) != 0; } 
public override bool Equals(object obj) { return CompareTo(this, obj as Natural) == 0; }
public bool Equals(Natural x) { return CompareTo(this, x) == 0; }

private static int CompareTo(Natural x, Natural y) { ... }

在您的控制器上

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

答案 1 :(得分:3)

使用包含类

的包装类
public class CustomerViewModel
{
    public CustomerFoodModel FoodModel { get; set; }
    public CustomerHistoryModel HistoryModel { get; set; }
}

答案 2 :(得分:3)

我认为最好的方法是在主视图中使用局部视图。局部视图可以回调另一个控制器以获取新模型并将该模型传递给局部视图。这可以使事情更好地分离。

请查看此帖以了解类似问题。 Using partial views in ASP.net MVC 4

答案 3 :(得分:1)

您有几个选择。我可能会创建一个包含两个模型的视图模型:

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

或者,根据您的数据结构,每个客户可能有多个历史记录条目:

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

然后,您的getCustomerDetails函数会返回CustomerViewModel