如何在Asp.Net MVC动态插入部分视图

编程入门 行业动态 更新时间:2024-10-26 22:29:27
本文介绍了如何在Asp.Net MVC动态插入部分视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我迁移Web表单网站MVC。在我的WebForms网站我有一个利用用户控件的各种组合页面,那么HTML的块,然后标签,文本框等。

I am migrating a Webforms site to MVC. In my webforms site I have pages that utilise various combinations of User Controls, then chunks of html then Labels, Textboxes etc.

我不想硬连线的每一页,所以我打算带动输出从CMS每一页它指定的顺序插入控件到页面中。

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.

我想每个控制现在将在MVC中的局部视图。 (我知道这是不正确的)。

I imagine each control will now be a Partial View in MVC. (Let me know if that's not correct).

所以,如果我有两个不同的部分景色,ViewA和ViewB,我怎么创建插入部分景致成是由CMS对于一个给定的URL确定的顺序返回的视图控制器的方法?

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?

因此​​,假设控制器方法被调用的报告,它需要一个叫做产品的参数。

So assuming the controller method is called Reports and it takes a parameter called product.

如// mysite的/报告?产品= A返回包含ViewA,ViewA,ViewB,ViewA视图

eg //MySite/Reports?product=A returns a view containing ViewA, ViewA, ViewB, ViewA

,而

// mysite的/报告?产品= B返回一个包含ViewA,ViewB,ViewA,视图ViewB等

//MySite/Reports?product=B returns a view containing ViewA, ViewB, ViewA, ViewB etc

所以应该在code是怎样的控制器的方法?

So what should the code be for the controller method?

我希望是有道理的。

推荐答案

如果我理解正确这应该解决您的问题。

If I understood you correctly this should solve your problem

刚刚创建PartialViewResult它接受多个视图名称,使它们派生新类。并使其多了几分可用创建一个新的扩展方法控制器打电话给你定制的ViewResult。

Just create a new class derived from PartialViewResult which accepts multiple view names to render them. And to make it a little more usable create a new extension method for the controller to call your customized ViewResult.

这是为我工作。您可以使用它如此简单:

That worked for me. You can use it so simply:

public ActionResult Index() { return this.ArrayView(new string[] { "ViewA", "ViewB" }); }

要使其工作ArrayViewResult类应该是:

To make it work ArrayViewResult class should be:

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); } } }

扩展方法:

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 }; } } }

更多推荐

如何在Asp.Net MVC动态插入部分视图

本文发布于:2023-11-15 10:38:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1593682.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   动态   如何在   Net   Asp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!