0

我的主屏幕的視圖模型爲DashboardViewModel。它具有自己的ViewModels的PartialViews,如CustomerSearchViewModelSelectProductViewModel部分視圖,視圖模型與主視圖不同

所有三個ViewModels是分開的。

當我運行該應用程序我得到這個錯誤:

The model item passed into the dictionary is of type 'Invoice.Web.ViewModels.DashboardViewModel', but this dictionary requires a model item of type 'Invoice.Web.ViewModels.SearchCustomerWindowVM'.

我不知道我應該怎麼做才能解決這個問題。

按照計劃,主屏幕最終會將很多PartialView與他們自己的視圖模型進行整合。我是否在DashboardViewModel內聲明瞭局部視圖模型?或者我只是有一個大的DashboardViewModel所有partialViews來?

回答

1

您可以將部分視圖視圖模型作爲主視圖模型的屬性,並調用Html.Partial並傳遞這些屬性。

public class DashBoardVM 
{ 
    public string Name { set;get;} 
    public CustomerSearchVM CustomerSearch { set; get;} 

    public DashBoardVM() 
    { 
    CustomerSearch =new CustomerSerachVM(); 
    } 
} 

在你的儀表板視圖,

@model DashBoardVM 
<h2>@Model.Name</h2> 
@Html.Partial("CustomerSearch",Model.CustomerSearch) 

假設CustomerSearch局部視圖是強類型ØCustomerSearchVM類。

+0

謝謝你這個工作;雖然CustomerSearch不是強類型的,但是它有一個PartialView(用於GridSearchResults),它是強類型化到CustomerSearchVM的。 –