Exemplo n.º 1
0
        private static string RenderViewToString(ControllerContext context,
                                                 string viewName, object viewData)
        {
            //Create memory writer
            var sb        = new StringBuilder();
            var memWriter = new StringWriter(sb);

            //Create fake http context to render the view
            var fakeResponse          = new HttpResponse(memWriter);
            var fakeContext           = new HttpContext(HttpContext.Current.Request, fakeResponse);
            var fakeControllerContext = new ControllerContext(
                new HttpContextWrapper(fakeContext),
                context.RouteData, context.Controller);

            var oldContext = HttpContext.Current;

            HttpContext.Current = fakeContext;

            //Use HtmlHelper to render partial view to fake context
            var html = new HtmlHelper(new ViewContext(fakeControllerContext,
                                                      new FakeView(), new ViewDataDictionary(), new TempDataDictionary(), memWriter),
                                      new ViewPage());

            RenderPartialExtensions.RenderPartial(html, viewName, viewData);

            //Restore context
            HttpContext.Current = oldContext;

            //Flush memory and return output
            memWriter.Flush();
            return(sb.ToString());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Renders partial view using an expression as prefix
        /// </summary>
        public static void RenderPartialFor <TModel, TProperty>(this HtmlHelper <TModel> helper, string partialViewName, Expression <Func <TModel, TProperty> > expression, object parameters = null)
        {
            if (helper == null)
            {
                throw new ArgumentNullException("helper");
            }

            var expressionText = ExpressionHelper.GetExpressionText(expression);
            var model          = helper.ViewData.GetViewDataInfo(expressionText);

            var originalPrefix = helper.ViewData.TemplateInfo.HtmlFieldPrefix;

            try
            {
                var viewData = new ViewDataDictionary(helper.ViewData);
                viewData.TemplateInfo.HtmlFieldPrefix = (string.IsNullOrEmpty(originalPrefix) ? "" : originalPrefix + ".") + expressionText;
                if (parameters != null)
                {
                    foreach (var pair in new RouteValueDictionary(parameters))
                    {
                        viewData[pair.Key] = pair.Value;
                    }
                }

                RenderPartialExtensions.RenderPartial(helper, partialViewName, model.Value, viewData);
            }
            finally
            {
                helper.ViewData.TemplateInfo.HtmlFieldPrefix = originalPrefix;
            }
        }
Exemplo n.º 3
0
 public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName, object model = null, ViewDataDictionary viewData = null)
 {
     RenderPartialExtensions.RenderPartial(
         htmlHelper,
         partialViewName,
         model,
         viewData ?? htmlHelper.ViewData);
 }
 public static Task RenderPartialAsync(this HtmlHelper htmlHelper, string partialViewName, object model = null, ViewDataDictionary viewData = null)
 {
     return(RenderPartialExtensions.RenderPartialAsync(
                htmlHelper,
                partialViewName,
                model,
                viewData ?? htmlHelper.ViewData));
 }
Exemplo n.º 5
0
        /// <summary>
        /// Renders partial view passing parameters into ViewData
        /// </summary>
        public static void RenderPartial(this HtmlHelper helper, string partialViewName, object model, object parameters)
        {
            var viewData = new ViewDataDictionary(helper.ViewData);

            if (parameters != null)
            {
                foreach (var pair in new RouteValueDictionary(parameters))
                {
                    viewData[pair.Key] = pair.Value;
                }
            }

            RenderPartialExtensions.RenderPartial(helper, partialViewName, model, viewData);
        }
Exemplo n.º 6
0
 public void RenderPartial(string partialViewName)
 {
     RenderPartialExtensions.RenderPartial(this, partialViewName);
 }