MVC - 动态加载的局部视图 [英] MVC - Dynamically loading Partial Views

查看:674
本文介绍了MVC - 动态加载的局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过传递路径的列表我想要的部分景色,然后在每个调用的RenderPartial动态加载的局部视图到视图。这似乎这样的伎俩。问题出现时,我尝试将模型传递到局部视图。由于我动态加载他们,我不知道到底通过为特定的局部视图哪种模式。我不想来填充每一个可能的对象,我使用反射与配置查找每个局部视图动态地加载模型考虑。我还在考虑增加一个ActionFilter这将自动填充为我,但即使这种实现正确的模型值将不得不使用反射。没有人有任何其他建议?

I'm trying to dynamically load partial views into a view by passing the list of paths for the partial views I want and then calling RenderPartial on each. This seems to do the trick. The problem comes in when I try to pass the model to the partial view. Since I'm dynamically loading them, I don't exactly know which model to pass for that particular partial view. I don't want to populate every possible object and I'm considering using reflection with a config lookup for each partial view to dynamically load the model. I was also considering adding an ActionFilter which would automatically populate the right model values for me but even this implementation would have to use Reflection. Does anyone have any other suggestions?

有一件事我想念我不MVC看到常规ASP.NET用户控件。用户控件封装自己的逻辑进行数据检索,而在MVC中,主控制器需要了解它。这意味着如果我使用的局部视图为另一个控制器,该控制器还必须知道如何获取模型为局部视图。我失去了一些东西在这里?谢谢你。

One thing I miss about regular ASP.NET user controls that I don't see in MVC. The user controls encapsulated their own logic for data retrieval whereas in MVC, the main controller needs to know about it. That means if I were to use a partial view for another controller, that controller would also have to know how to fetch the model for that partial view. Am I missing something here? Thanks.

推荐答案

如果你想要做你的描述的能力,你可能有兴趣阅读的 Html.RenderAction()。这是在许多情况下有用,但不是纯粹的MVC(也许是务实的MVC)。

If you want the ability to do what you describe, you might be interested in reading about Html.RenderAction(). This is useful in many circumstances but is not "pure" MVC (perhaps pragmatic MVC).

我有一个类似的情况给你在哪里我使用的局部视图加载不同的搜索屏幕(搜索提交按钮都一样),只是表单域是不同的。

I have a similar situation to you where I am using partial views to load different search screens (the search submission buttons are the same), just the form fields are different.

我做到这一点在下面的方式。在一个公共基类控制器我有泛型类型参数,这是我传递到视图模型对象(SearchObject是对象类型)。

I do it in the following way. In a common base class controller I have generic type parameter, which I pass to a view model object (SearchObject is of type object).

public abstract class SampleController<T>  : Controller where T : new()
public virtual ActionResult SearchForDocuments()
        {
            searchModel.SearchObject = // Create type of T;
            // Some more code
            return View("SomeView", searchModel);
        }

然后我有一个强类型视图,它传递SearchObject的局部视图。

I then have a strongly typed view, which passes the SearchObject to the partial view.

 <% Html.RenderPartial(@"../Search/SearchCriteriaTemplates/" + /*Specific view name*/, Model.SearchObject); %>

局部视图,然后是强类型,并知道如何处理强类型的模型。

The partial view then is strongly typed and knows what to do with the strongly type Model.

这篇关于MVC - 动态加载的局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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