Wednesday, December 21, 2016

Multiple Partial view return MVC


Step 1: Create class CustomJsonResult

public class CustomJsonResult : JsonResult
    {
        public CustomJsonResult(object json, List<string> errors)
        {
            _json = json;
            _partials = new List<KeyValuePair<string, object>>();
            _results = new List<string>();
            _errors = errors;
        }
        private readonly object _json;
        private readonly IList<KeyValuePair<string, object>> _partials;
        private readonly IList<string> _results;
        private readonly IList<string> _errors;

        public CustomJsonResult WithHtml(string partialViewName = null, object model = null)
        {
            _partials.Add(new KeyValuePair<string, object>(partialViewName, model));
            return this;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            foreach (var partial in _partials)
            {
                var html = RenderPartialToString(context, partial.Key, partial.Value);
                _results.Add(html);
            }
            base.Data = Data;
            base.ExecuteResult(context);
        }

        public new object Data
        {
            get
            {
                return new
                {
                    Html = _results,
                    Json = _json,
                    errors = _errors
                };
            }
        }

        public static string RenderPartialToString(ControllerContext context, string viewName, object model)
        {
            var controller = context.Controller;

            var partialView = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);

            var stringBuilder = new StringBuilder();
            using (var stringWriter = new StringWriter(stringBuilder))
            {
                using (var htmlWriter = new HtmlTextWriter(stringWriter))
                {
                    controller.ViewData.Model = model;
                    partialView.View.Render(
                        new ViewContext(
                            controller.ControllerContext,
                            partialView.View,
                            controller.ViewData,
                            new TempDataDictionary(),
                            htmlWriter),
                        htmlWriter);
                }
            }
            return stringBuilder.ToString();
        }
    }

Step 2:Create virtual method

 protected internal virtual CustomJsonResult CustomJson(object json = null, bool allowGet = true, List<string> errors = null)
        {
            return new CustomJsonResult(json, errors)
            {
                JsonRequestBehavior = allowGet ? JsonRequestBehavior.AllowGet : JsonRequestBehavior.DenyGet,
                MaxJsonLength = Int32.MaxValue
            };
        }

Step 3: How to call
you can pass other data as well
var json="Test" ;
return CustomJson(json).WithHtml("<partial view name>", <Your Data>).WithHtml("<partial view name>", <Your Data>);