如何通过视图模型属性作为模型的局部视图? [英] How to pass ViewModel property as model to partial view?

查看:174
本文介绍了如何通过视图模型属性作为模型的局部视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在视图模型称为至极我LogViewModel有3个特性如下图所示。

I have ViewModel called LogViewModel in wich i have 3 properties as shown below.

public class LogViewModel
{
    public IEnumerable<Log> Logs { get; private set; }
    public PaginationHelper Pagination { get; set; }
    public LogFilter Filter { get; set; }
}

LogViewModel传递以查看模型。
现在,我需要通过LogFilter(数据),以局部视图,如:

LogViewModel is passed to View as Model. Now i need to pass LogFilter (with data) to partial view like:

@Html.Partial("_LogsFilter", Model.Filter)

我尝试了很多方法,但总是得到同样的错误:

I tried a lot of methods but always get the same error:

传递到字典中的模型产品类型
  基础设施。presentation.Desk.ViewModels.LogViewModel',
  但本词典需要类型的模型项目
  基础设施。presentation.Desk.Models.LogFilter。

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'.

有什么想法?

推荐答案

由于过滤器属性为null,框架会从父视图的模型传递到局部,这是 LogViewModel ,而局部视图期待的类型 LogFilter

Because Filter property is null, the framework will pass the model from the parent view to the partial, which is LogViewModel, while partial view is expecting type of LogFilter.

要prevent这确保过滤器属性渲染视图之前实例化,或做这样的事情:

To prevent this assure that Filter property is instantiated before rendering the view, or do something like this:

@Html.Partial("_LogsFilter", Model.Filter ?? new LogFilter())

这篇关于如何通过视图模型属性作为模型的局部视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆