[英]How To Insert Partial views Dynamically in Asp.Net MVC
I am migrating a Webforms site to MVC. 我正在将Webforms站点迁移到MVC。 In my webforms site I have pages that utilise various combinations of User Controls, then chunks of html then Labels, Textboxes etc.
在我的Webforms站点中,我的页面利用了用户控件的各种组合,然后是html块,然后是Labels,Textboxes等。
I don't want to hardwire each page so I am going to drive the output for each page from a CMS which specifies the order to insert the controls into the page. 我不想对每个页面进行硬连线,所以我要从CMS驱动每个页面的输出,该CMS指定将控件插入页面的顺序。
I imagine each control will now be a Partial View in MVC. 我想每个控件现在将成为MVC中的部分视图。 (Let me know if that's not correct).
(让我知道是否不正确)。
So if I have two different partial views, ViewA and ViewB, how do I create a controller method that inserts the partial views into the view that is returned in the order determined by the CMS for a given url? 因此,如果我有两个不同的局部视图,即ViewA和ViewB,如何创建一个控制器方法,该方法将局部视图插入按CMS为给定URL确定的顺序返回的视图中?
So assuming the controller method is called Reports and it takes a parameter called product. 因此,假设控制器方法称为Reports,并且它采用一个称为product的参数。
eg //MySite/Reports?product=A returns a view containing ViewA, ViewA, ViewB, ViewA 例如// MySite / Reports?product = A返回一个包含ViewA,ViewA,ViewB,ViewA的视图
whereas 而
//MySite/Reports?product=B returns a view containing ViewA, ViewB, ViewA, ViewB etc // MySite / Reports?product = B返回包含ViewA,ViewB,ViewA,ViewB等的视图
So what should the code be for the controller method? 那么该代码应用于控制器方法?
I hope that makes sense 我希望这是有道理的
If I understood you correctly this should solve your problem 如果我对您的理解正确,那应该可以解决您的问题
Just create a new class derived from PartialViewResult which accepts multiple view names to render them. 只需创建一个从PartialViewResult派生的新类即可,该类接受多个视图名称来呈现它们。 And to make it a little more usable create a new extension method for the controller to call your customized ViewResult.
为了使它更有用,请为控制器创建一个新的扩展方法,以调用您自定义的ViewResult。
That worked for me. 那对我有用。 You can use it so simply:
您可以简单地使用它:
public ActionResult Index()
{
return this.ArrayView(new string[] { "ViewA", "ViewB" });
}
To make it work ArrayViewResult class should be: 为了使其工作,ArrayViewResult类应为:
public class ArrayViewResult : PartialViewResult
{
public IEnumerable<string> Views;
protected override ViewEngineResult FindView(ControllerContext context)
{
return base.FindView(context);
}
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
if (!Views.Any())
throw new Exception("no view...");
TextWriter writer = context.HttpContext.Response.Output;
foreach(var view in Views)
{
this.ViewName = view;
ViewEngineResult result = FindView(context);
ViewContext viewContext = new ViewContext(context, result.View, ViewData, TempData, writer);
result.View.Render(viewContext, writer);
result.ViewEngine.ReleaseView(context, result.View);
}
}
}
Extension method: 扩展方式:
namespace System.Web.Mvc
{
public static class ArrayViewResultExtension
{
public static ArrayViewResult ArrayView(this Controller controller, string[] views)
{
return ArrayView(controller, views, null);
}
public static ArrayViewResult ArrayView(this Controller controller, string[] views, object model)
{
if (model != null)
{
controller.ViewData.Model = model;
}
return new ArrayViewResult
{
ViewName = "",
ViewData = controller.ViewData,
TempData = controller.TempData,
ViewEngineCollection = controller.ViewEngineCollection,
Views = views
};
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.