是否有一个相当于单轨视图组件的ASP.Net MVC框架? [英] Is there an equivalent to Monorail view components for the ASP.Net MVC Framework?

查看:218
本文介绍了是否有一个相当于单轨视图组件的ASP.Net MVC框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我大量使用视图的组件在一些规模较大的应用程序,我建单轨的 - 什么是在ASP.Net MVC视图组件相当于方法,即可以支持部分等。

I make heavy use of View Components in some of the larger applications I've built in Monorail - What is the equivalent approach in ASP.Net MVC for a view component, that can support sections etc.?

推荐答案

其实你有几个选项来创建ASP.NET MVC一个ViewComponent相当于,这取决于在你的组件的复杂性。我使用的是我知道的选项更MVC上下的这两种方法。

Actually you have several options to create the equivalent of a ViewComponent in ASP.NET MVC, depending in the complexity of your component. I use these two approaches which are the more mvc-ish of the options I am aware of.

1
最简单的事情就是创建一个ViewUserControl并使用Html.RenderPartial用辅助显示。该ViewUserControl是一段简单的标记,没有后盾控制器(我想你可以把codebehind文件,如果你想)。
或者,您可以调用的RenderPartial的时候,像这样通过一个模型对象或整个ViewData字典的观点:

1: The simplest thing is to create a ViewUserControl and display it using Html.RenderPartial with the helper. The ViewUserControl is a simple piece of markup with no backing controller (I think you can put a codebehind file if you want). Optionally, you can pass a model object or the entire ViewData dictionary to the view when calling RenderPartial, like this:

<% Html.RenderPartial("TopBar", model); %>

顶栏是一个ASCX页。这个工程的任何地方,在母版页和正常的看法。

"TopBar" is an ascx page. This works anywhere, in master pages and in normal views.

2:
如果你想让你的分量有更复杂的逻辑或访问数据源,海委会,等等,那么你可以使用Html.RenderAction这是在Microsoft.Web.Mvc装配发现的扩展方法。我用这出mvccontrib分布。它的工作原理是这样,你需要与你的需要,然后创建一些看法和所有这些东西变成你的组成部分,所有的逻辑创建一个普通的控制器,例如:

2: If you want your component to have more complicated logic or to access datasources, IoC, etc, then you can use Html.RenderAction which is an extension method found in the Microsoft.Web.Mvc assembly. I am using this out of the mvccontrib distribution. It works like this, you need to create a normal controller with all the logic you need, then create some views and all of these things become your component, for example:

public class AboutComponentController : Controller {
public IRepository Repository{ get; set; }

public ActionResult Detail() {
	var lastEvent = Repository.FindAll<Auditoria>().FirstOrDefault();
	return View(lastEvent);
}

}

注意我是如何得这将是(对我来说温莎)国际奥委会注入一个IRepository的引用,我可以做任何一个正常的控制器会怎么做。

Notice how I have a reference to an IRepository which is going to be injected with IoC (Windsor in my case) and I can do anything a normal controller would do.

现在,在您要使用你的组件,进口Microsoft.Web.Mvc并调用Html.RenderAction适当的参数的任何页面(主或正常)。这将创建一个小型MVC管道创建控制器,解决了视图等,就像一个单轨ViewComponent。我preFER使用的方法的拉姆达基于变化,如:

Now, in any page (master or normal) where you want to use your component, import Microsoft.Web.Mvc and call Html.RenderAction with the appropriate parameters. This will create a mini mvc pipeline that creates the controller, resolves the view, etc., just like a Monorail ViewComponent. I prefer to use the lambda based variation of the method, like this:

<% Html.RenderAction<AboutComponentController>(x => x.Detail("a message"));%>

不幸的是,传递参数的唯一方法是使用该方法调用本身,而这又必须在控制器是唯一的。还需要一些工作,像一个ViewComponent。

Unfortunately, the only way to pass parameters is to use the method call itself, which in turn must be unique in the controller. Still needs some work to resemble a ViewComponent.

我不使用masterpages或布局在我的组件的看法,因为它们是组成元素本身。

I don't use masterpages or layouts in the views of my components since they are composition elements themselves.

请记住,使用Web表单视图引擎时,你可以有强类型的意见,如果你想拥有智能感知在code块使用模型变量时。

Remember that when using the Webforms view engine, you can have strongly typed views if you like to have intellisense when using the Model variable in code blocks.

这样做的好处是,你可以用这些方法混合使用视图引擎,我通常在nvelocity创建组件和aspx页面显示出来,等等。

The beauty of this is that you can mix view engines with these approaches, I usually create the components in nvelocity and display them in aspx pages, etc.

我现在可以用的部分景色缓存问题,但我还没有遇到任何至今。我相信还有其他的选择(如在mvccontrib子控制器),但是这通常是不够的简单情况。当然你也可以使用普通的ASP.net组件的ASPX视图页面,但是这将是欺骗吗?嘿嘿。我希望它能帮助。

I now there can be issues with caching of the partial views but I haven't run into any so far. I am sure there are other options (like subcontrollers in mvccontrib) but this is usually enough for simple cases. Of course you can use normal ASP.net components in your aspx view pages but that would be cheating right? hehe. I hope it helps.

这篇关于是否有一个相当于单轨视图组件的ASP.Net MVC框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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