我有ViewModel稱爲LogViewModel至至此我有3屬性如下所示。如何將ViewModel屬性作爲模型傳遞給部分視圖?
public class LogViewModel
{
public IEnumerable<Log> Logs { get; private set; }
public PaginationHelper Pagination { get; set; }
public LogFilter Filter { get; set; }
}
LogViewModel被傳遞給View as Model。 現在我需要LogFilter(數據)傳遞給局部視圖,如:
@Html.Partial("_LogsFilter", Model.Filter)
我嘗試了很多方法,但總是得到同樣的錯誤:
The model item passed into the dictionary is of type 'Infrastructure.Presentation.Desk.ViewModels.LogViewModel', but this dictionary requires a model item of type 'Infrastructure.Presentation.Desk.Models.LogFilter'.
有什麼想法?
你的'Filter'屬性可能爲null。嘗試像'@ Html.Partial(「_ LogsFilter」,Model.Filter?new LogFilter())'',當然如果'LogFilter'有一個默認的構造函數。 – Zabavsky
是你的視圖的'ModelType'聲明'@model LogFilter'而不是'@model LogViewModel'嗎? –
@Zabavsky謝謝你的工作。我在LogViewModel構造函數中創建了一個LogFilter的新實例,然後意外地將它的值設置爲null。 –