Exemplo n.º 1
0
        /// <summary>
        /// Renders a Razor view from an embedded resource.
        /// </summary>
        /// <param name="helper">MVC HTML helper instance.</param>
        /// <param name="resourceName">Embedded resource name.</param>
        /// <param name="modelType">Type of model.</param>
        /// <param name="model">Model instance.</param>
        /// <returns>Rendered resource as an MvcHtmlString.</returns>
        /// <remarks>
        /// If needed by view, define ViewBag.DataContext before rendering resource.
        /// </remarks>
        public static MvcHtmlString RenderResource(this HtmlHelper helper, string resourceName, Type modelType = null, object model = null)
        {
            MvcHtmlString  result;
            DynamicViewBag viewBag = new DynamicViewBag(helper.ViewData);
            bool           isPost  = HttpContext.Current.Request.HttpMethod.Equals("POST", StringComparison.OrdinalIgnoreCase);

            viewBag.AddValue("IsPost", isPost);
            viewBag.AddValue("Request", HttpContext.Current.Items["MS_HttpRequestMessage"] ?? new HttpRequestMessage(isPost ? HttpMethod.Post : HttpMethod.Get, HttpContext.Current.Request.Url));

            if (FilePath.GetExtension(resourceName).Equals(".vbhtml", StringComparison.OrdinalIgnoreCase))
            {
                result = new MvcHtmlString(RazorEngine <VisualBasicEmbeddedResource> .Default.RunCompile(resourceName, modelType, model, viewBag));
            }
            else
            {
                result = new MvcHtmlString(RazorEngine <CSharpEmbeddedResource> .Default.RunCompile(resourceName, modelType, model, viewBag));
            }

            // Copy any new or updated view bag elements back to MVC view data dictionary
            foreach (string memberName in viewBag.GetDynamicMemberNames())
            {
                object value;

                viewBag.TryGetMember(new MemberBinder(memberName), out value);

                if (value != null)
                {
                    helper.ViewData[memberName] = value;
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取所有成员名称
        /// </summary>
        public List <string> GetKeys_default()
        {
            var arr = viewBag.GetDynamicMemberNames();

            if (arr != null)
            {
                return(arr.ToList());
            }
            return(new List <string>());
        }
Exemplo n.º 3
0
        // Renders the partial view with a copy of the given view data plus the given model
        /// <summary>
        /// 部分视图
        /// </summary>
        /// <param name="partialViewName">部分视图名称</param>
        /// <param name="controllerName">控制器名称</param>
        /// <param name="model"> model</param>
        /// <param name="ViewBag">ViewBag</param>
        /// <returns></returns>
        public IEncodedString RenderPartial(string partialViewName, string controllerName, object model, DynamicViewBag ViewBag)
        {
            RefRequestEntity paras = SetParamValue(model);

            var    t          = HuberHttpModule.CurDomainAssembly.GetType(HuberHttpModule.CurDomainAssemblyName + ".Controllers." + controllerName + "Controller");
            var    ActionFunc = t.GetMethod(partialViewName);
            object dObj       = Activator.CreateInstance(t);

            var AddViewBageFunc = t.GetMethod("AddViewBageValues");

            foreach (string key in ViewBag.GetDynamicMemberNames())
            {
                AddViewBageFunc.Invoke(dObj, new object[] { key, Impromptu.InvokeGet(ViewBag, key) });
            }

            object result = ActionFunc.Invoke(dObj, new object[] { paras });

            return(new RawString((result as RefRespondEntity).ResultContext.ToString()));
        }