English 中文(简体)
ASP.NET MVC ViewPage rendered in a ViewPage
原标题:

I m wondering if it is possible to render a ViewPage inside of a ViewPage.

Normally, you d have this:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<my_object>"%>

and then in the page you can render a user control:

<%Html.RenderPartial("~/Views/Shared/mycontrol.ascx", this.ViewData);%>

However I d like to render a ViewPage in a ViewPage. The reason? I m working on a very large project with 10+ developers and a lot of code already written. A large piece is already written which dynamically renders the UI however there are 15+ methods which pass around a ViewPage.

In other words, I m calling this in the ViewPage:

<%this.RenderUI();

And the method stub is this:

public static void RenderUI(this ViewPage viewPage)

So in order to reuse the most code, I d like to pull the call to this.RenderUI() into it s own ViewPage but then include that on some ViewPages. It s a hairy situation and it sounds unnecessary, but there s a lot of code rework that would need to be done otherwise. Does anyone know if this can be achieved?

最佳回答

Render your subview in the main view using RenderAction() instead of RenderPartial(). This allows you to keep your controller and view for each subview, and "inject" the output of each controller/subview combination into any main view, in a location of your choosing within the main view.

This arrangement should ease some of the complexity of managing each of the views and subviews. It is an approach reminiscent of the "web parts" in traditional ASP.NET or SharePoint. It is a more modular approach that provides for better reusability.

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签