コード例 #1
0
        /// <summary>
        /// Renders the given partial in the context of the given property.
        /// Use PartialFor(m => m) to render a partial for the model itself rather than a child property.
        /// </summary>
        /// <typeparam name="TModel">The form model type</typeparam>
        /// <typeparam name="TPartialModel">The type of the model property to use for the partial model</typeparam>
        /// <param name="section">The current section</param>
        /// <param name="partialModelProperty">The property to use for the partial model</param>
        /// <param name="partialViewName">The name of the partial view to render</param>
        /// <returns>The HTML for the rendered partial</returns>
        public static Task <IHtmlContent> PartialForAsync <TModel, TPartialModel>(this ISection <TModel> section, Expression <Func <TModel, TPartialModel> > partialModelProperty, [AspMvcPartialView] string partialViewName)
        {
            var formModel = section.Form.HtmlHelper.ViewData.Model;

            using (var h = section.Form.HtmlHelper.For(partialModelProperty, bindFieldsToParent: true))
            {
                using (var f = section.Form.CreatePartialForm(partialModelProperty, h))
                {
                    using (section.CreatePartialSection(f))
                    {
                        return(h.PartialAsync(partialViewName, partialModelProperty.Compile().Invoke(formModel), h.ViewData));
                    }
                }
            }
        }