渲染code MVC剃刀管窥 [英] Rendering Partial View in code MVC Razor

查看:96
本文介绍了渲染code MVC剃刀管窥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC 3剃须刀做一个简单的CMS实践的目的,这种想法是,我创建了一些局部视图。

I'm using MVC 3 Razor to make a simple CMS for practice purposes, and the idea is that I'm creating a few partial views.

我想要做一个数据库查询,并看到3部分景色需要渲染的页面。

I'm wanting to do a database lookup, and see that 3 partial views need rendering to the page.

我会怎么做呢?在的WebForms,调用LoadControl(ControlURL),但我没看到一个等同的。

How would I do this? In WebForms, you call the LoadControl(ControlURL), but I don't see an equivalent here.

难道是一个客户端的事情吗?

Would it be a client side thing?

编辑 - 我是提前从模型中取一个视图名称,然后渲染视图,而不是知道视图的名称更多的思考。因此,一个页面可能有一个名为Foo视图或命名的酒吧视图。该模型,在运行时会告诉控制器动作的视图渲染。

Edit - I was more thinking of taking a View name from the model, and then rendering that view rather than knowing the name of the view in advance. So a page might have a view named Foo or a view named Bar. The model, at run time will tell the controller action which view to render.

推荐答案

有,你可以用它来呈现的控制两种方法。

There are two methods that you can use to render a "control".

@Html.Partial("ViewName")
@{ Html.RenderPartial("ViewName"); }

您也可以使其他操作。

@Html.Action("ActionName", "Controller", new { Values = "yourvalues" })
@{ Html.RenderAction("ActionName", "Controller", new { Values = "yourvalues" }); }

请注意每一个由 @ {} 这是因为他们不返回一个字符串,而是直接呈现到流。

Notice the second of each one is surrounded by @{ } this is because they do not return a string but render directly to the stream.

这篇关于渲染code MVC剃刀管窥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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