如何从视图的局部视图传递模型? [英] How to pass Model from a view to a partial view?

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

问题描述

我有一个观点,即是不可以强类型。不过,我在这个认为是强类型的局部视图。

I have a view that is not strongly typed. However I have in this view a partial view that is strongly typed.

我怎么做我通过模型这种强类型的观点?

How do I do I pass the model to this strongly typed view?

我想是这样

 public ActionResult Test()
        {
              MyData = new Data();
              MyData.One = 1;
              return View("Test",MyData)
        }

在我TestView

In my TestView

<% Html.RenderPartial("PartialView",Model); %>

这给我一个计算器例外。所以,我不知道如何通过它。当然,我不希望让测试来看,如果可能的强类型,就好像我有过10强类型的局部视图在该视图我需要像某种包装发生了什么。

This give me a stackoverflow exception. So I am not sure how to pass it on. Of course I don't want to make the test view strongly typed if possible as what happens if I had like 10 strongly typed partial views in that view I would need like some sort of wrapper.

推荐答案

您应该扩展你的模型,以便它可以提供对视图中的所有必要的字段(这就是所谓的视图模型),或者单独为他们提供的ViewData。

You should extend your model so that it can provide all necessary fields for the view (this is called ViewModel) or you provide them seperately with ViewData.

 public ActionResult Test()
        {
              MyData = new Data();
              MyData.One = 1;
              ViewData["someData"]=MyData;
              return View();
        }

然后:

<% Html.RenderPartial("PartialView",ViewData["someData"]); %>

ViewData的是一个很好的losely类型的字典

ViewData is a nice losely typed dictionary

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

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