2016-06-29 39 views
-1

我必須創建一個發票視圖。我有很多模型(表格),我希望在發票的一個視圖中顯示來自多個模型的所有數據。我創建了一個空的(沒有模型)視圖並放入了部分視圖。其中的局部視圖返回一個視圖與一個模型,但這種解決方案還給我例外...這是我的工作: 這是我在控制器操作:在單個視圖中顯示多個模型

public ActionResult Customer() 
     { 
      var data = db.Customer; 

      return PartialView("Index", data); 
     } 

     public ActionResult Invoice() 
     { 
      var data = db.Invoice; 

      return PartialView("Index", data); 
     } 

     public ActionResult Dealer() 
     { 
      var data = db.Dealer; 

      return PartialView("Index", data); 
     } 

     public ActionResult Paid() 
     { 
      var data = dane.Paid; 

      return PartialView("Index", data); 
     } 

     public ActionResult Products() 
     { 
      var data = dane.Products; 

      return PartialView("Index", data); 
     } 

這是局部視圖之一:

@model IEnumerable<Invoice_v1._0.Models.Products> 

<table class="table"> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.Name) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Price) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Amount) 
     </th> 
     <th></th> 
    </tr> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.Name) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Price) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Amount) 
     </td> 
    </tr> 
} 

</table> 

這是我的「索引」視圖,局部視圖:

@Html.Partial("_Customer") 
@Html.Partial("_Invoice") 
@Html.Partial("_Dealer") 
@Html.Partial("_Paid") 
@Html.Partial("_Products") 

我該如何解決這些問題?

+1

什麼/異常在哪裏? –

+0

使用'@ Html.Action()'是你想調用一個控制器方法,返回一個包含數據的局部視圖。 (或使用包含所有填充數據的視圖模型) –

+0

我還注意到,僅僅因爲在視圖中有不同的「表」,沒有必要使用部分視圖。這種做法的慣用方法是創建一個ViewModel,該ViewModel包含視圖所需的所有數據,並將其用作視圖的「模型」。部分視圖更適用於可放入不同父視圖內的_reusable_視圖。 –

回答

2

如果你堅持有一個單一的視圖,那麼你可以讓有條件的局部視圖渲染:

if (Model != null && Model is Customer) 
    Html.Partial("_Customer", Model as Customer) 

,等等。

在相關說明中,由於所有模型都是排他性的(你永遠不會有多個對象),所以我沒有看到有一個索引視圖的要點。你已經有了每個類的一個(部分)視圖,那麼爲什麼不把它們用作常規視圖呢?

更新

如果選擇從每個操作方法都返回獨立的觀點,那麼你可以做到這一點通過指定每個視圖的名稱。請參考此問題的詳細信息:How to return ActionResult with specific View (not the controller name)

你的具體情況,這將意味着:

public ActionResult Customer() 
{ 
    return View("_Customer", db.Customer); 
} 

public ActionResult Invoice() 
{ 
    return View("_Invoice", db.Invoice); 
} 

public ActionResult Dealer() 
{ 
    return View("_Dealer", db.Dealer); 
} 

public ActionResult Paid() 
{ 
    return View("_Paid", db.Paid); 
} 

public ActionResult Products() 
{ 
    return View("_Products", db.Products); 
} 

確保每個視圖需要一個強類型的模型。這將使實現這些視圖變得容易。

+0

你的意思是我應該創建一個定期的意見,而不是部分意見?但是,我怎樣才能在一個視圖(一頁)上顯示? – kenzolek

+0

我已添加更新到答案。 –

+0

我正在使用您的解決方案,但它在_Products部分視圖的foreach循環中返回模型中的「NullReferenceExcetion」。你知道爲什麼嗎? – kenzolek

0

儘管您的方法可能有效,但您對數據的控制會較少或無法控制(如果您想讓我們假設連接或使用任何父數據)。在這種情況下,我所做的是創建一個包含多個對象的ViewModel作爲它的一部分。例如

public class InvoiceViewModel 
{ 
    public InvoiceHead Header { get;set; } 
    public IList<InvoiceDetail> Details { get; set; } 
} 

因此,現在我可以在進入我的視圖之前填充此視圖模型。

public ActionResult Invoice(int id) 
{ 
    InvoiceViewModel viewModel = new InvoiceViewModel(); 
    viewModel.Header = db.InvoiceHead.SingleOrDefault(i => i.ID == id); 
    if(viewModel.Header != null) 
    { 
     viewModel.Details = db.Details.Where(d => d.Inv_id == id).ToList(); 
    } 
    return View(viewModel); 
} 

現在我可以用我的發票數據的部分視圖或整個視圖來設計我的視圖。

我將把作爲模型了IEnumerable或IList的,如果我使用部分兩者將是

@model db.Objects.InvoiceHead //用於報頭

@model IEnumerable的//爲詳情

0

在HTML代碼將是這樣的

@Html.Partial("~/Views/Customers/_AttachFileList.cshtml") 

使用自己的文件夾路徑。希望作品

相關問題