如何从通过其模型的局部视图prevent观点,而是传递null? [英] How to prevent view from passing its model to the partial view, but instead pass null?

查看:139
本文介绍了如何从通过其模型的局部视图prevent观点,而是传递null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET MVC和使用剃须刀,我有一个观点(父)调用另一个View(子)作为部分。无论是强类型的,但他们有不同的型号。

In ASP.NET MVC and using Razor, I have a View (parent) calling another View (child) as partial. Both are strongly typed but they have a different Model type.

通常情况下,在这种情况下,我们明确地传递模型从父视图子视图。

Normally, in these situations, we pass a Model explicitly from the parent View to the child View.

@Html.Partial("Child", Model)

我们也可以选择不明确指定要传递模型,在这些情况下,父视图会尝试自己的模式传递给子视图。如果类型相匹配,它是在大多数情况下,希望的工作原理。

We can also choose not to specify explicitly a Model to be passed and, in those cases, the parent View will attempt to pass its own Model to the child View. This works if the types match and it is desirable in most cases.

@Html.Partial("Child")

在我的情况,但是,我真正想要的父视图的的尝试任何东西传递给孩子查看。我会怎么做呢?

In my case, however, I really want the parent View not to try to pass anything to the child View. How would I do that?

我想尝试通过的明确:

@Html.Partial("Child", null)

但是,这还通过家长的模型子视图,从而导致错误:传递到字典的模型项的类型是'A',但本词典需要类型的模型项目 B'。

(作为一个方面说明,孩子View是它的模型创建视图,这就是为什么我没有一个实例来传递给它的。)

(As a side note, the child View is a Create View for its model, that's why I don't have an instance to pass to it.)

推荐答案

试试这个:

@Html.Partial("Child", null, new ViewDataDictionary<ChildType>(childInstance/*this can be null*/))

这篇关于如何从通过其模型的局部视图prevent观点,而是传递null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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