是否有可能使控制器外的看法? [英] Is it possible to render a view outside a controller?
问题描述
我想知道是否有可能呈现从一类是不是一个控制器视图。我看到的一切似乎是说,你不能。
我想要做的就是呈现来自一个WCF Web服务的局部视图,以推动它在其他地方。是否有可能使用的视图引擎是什么?
谢谢!
更新:
我不断收到空参数异常的的HtmlHelper。这里是我的code和堆栈跟踪。我的部分确实是一个名为TableOfContent.cshtml和位于/查看/共享文件夹。难道我新以不同的实例我ViewContext?
的HtmlHelper助手=新的HtmlHelper(新ViewContext(),可视数据);
变种一个= helper.Partial(TableOfContent);
在System.Web.Mvc.ViewContext..ctor(ControllerContext controllerContext,IVIEW观点,可视数据的ViewDataDictionary,TempDataDictionary TempData的,TextWriter的作家)
在System.Web.Mvc.HtmlHelper.RenderPartialInternal(字符串partialViewName,可视数据的ViewDataDictionary,对象模型,TextWriter的作家,ViewEngineCollection viewEngineCollection)
在System.Web.Mvc.Html.PartialExtensions.Partial(的HtmlHelper的HtmlHelper,字符串partialViewName,对象模型,可视数据的ViewDataDictionary)
在System.Web.Mvc.Html.PartialExtensions.Partial(的HtmlHelper的HtmlHelper,字符串partialViewName)
在SyncInvokeProcessEvent(对象,对象[],对象[])
在System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object实例,对象[]输入,对象[]&放大器;输出)
在System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&安培; RPC)
下面是两种不同的思路来考虑:
- 我一直使用 RazorEngine 做类似的东西。可以渲染剃刀模板字符串。
- 创建一个控制器和使用Web客户端调用的动作。这是假设你有一个MVC应用程序。
I wanted to know if it was possible to render a view from a class that is not a controller. Everything I see seems to say that you can't.
What I'm trying to do is to render a partial view from a WCF web service in order to push it somewhere else. Is it possible to use the view engine for that?
Thanks!
Update:
I keep getting argument null exception with the HtmlHelper. Here is my code and the stack trace. My partial is indeed named TableOfContent.cshtml and is located in the /View/Shared folder. Do I new to instantiate my ViewContext differently?
HtmlHelper helper = new HtmlHelper(new ViewContext(), viewData);
var a = helper.Partial("TableOfContent");
at System.Web.Mvc.ViewContext..ctor(ControllerContext controllerContext, IView view, ViewDataDictionary viewData, TempDataDictionary tempData, TextWriter writer)
at System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection)
at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData)
at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName)
at SyncInvokeProcessEvent(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
Here are two different ideas to consider:
- I've done something similar using RazorEngine. Allows you to render razor templates to a string.
- Create a controller and use WebClient to invoke the action. This assumes you have an MVC application.
这篇关于是否有可能使控制器外的看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!