创建控制器在ASP.NET MVC局部视图 [英] Create controller for partial view in ASP.NET MVC
问题描述
我怎样才能创建一个局部视图单个控制器和模型?我希望能够到任何地方把这个局部视图在网站上所以它需要它自己的控制器。我目前的渲染部分作为如此
@ Html.Partial(_见证)
为什么不使用<一个href=\"http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx\"><$c$c>Html.RenderAction()$c$c>?
然后,你可以把下列任何控制器(甚至为它创造一个新的控制器):
[ChildActionOnly]
公众的ActionResult MyActionThatGeneratesAPartial(字符串参数1)
{
VAR模型= repository.GetThingByParameter(参数1);
VAR partialViewModel =新PartialViewModel(模型);
返回PartialView(partialViewModel);
}
然后,你可以创建一个新的局部视图,有你的 PartialViewModel
是怎样从继承。
有关剃须刀,在视图中的code座是这样的:
@ {Html.RenderAction(指数,家); }
对于WebFormsViewEngine,它应该是这样的:
&LT;%Html.RenderAction(指数,家); %GT;
How can I create an individual controller and model for a partial view? I want to be able to place this partial view any where on the site so it needs it's own controller. I am current rendering the partial as so
@Html.Partial("_Testimonials")
Why not use Html.RenderAction()
?
Then you could put the following into any controller (even creating a new controller for it):
[ChildActionOnly]
public ActionResult MyActionThatGeneratesAPartial(string parameter1)
{
var model = repository.GetThingByParameter(parameter1);
var partialViewModel = new PartialViewModel(model);
return PartialView(partialViewModel);
}
Then you could create a new partial view and have your PartialViewModel
be what it inherits from.
For Razor, the code block in the view would look like this:
@{ Html.RenderAction("Index", "Home"); }
For the WebFormsViewEngine, it would look like this:
<% Html.RenderAction("Index", "Home"); %>
这篇关于创建控制器在ASP.NET MVC局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!