/// <summary> /// Show a partial view for a part of the view's model. /// </summary> /// <typeparam name="TModel">The type of the main model.</typeparam> /// <typeparam name="TField">The type of the part in the model.</typeparam> /// <param name="htmlHelper">The HTML helper.</param> /// <param name="partialViewName">The name of the partial view template.</param> /// <param name="modelPropertyExpression">An expression that defines the part inside the main model.</param> /// <returns>Returns the rendered partial view.</returns> /// <remarks> /// This method takes care of the model prefix which the partial view must include /// in its input field expressions. /// </remarks> public static Task <IHtmlContent> PartialForAsync <TModel, TField>( this IHtmlHelper <TModel> htmlHelper, string partialViewName, Expression <Func <TModel, TField> > modelPropertyExpression) { if (htmlHelper == null) { throw new ArgumentNullException(nameof(htmlHelper)); } if (partialViewName == null) { throw new ArgumentNullException(nameof(partialViewName)); } if (modelPropertyExpression == null) { throw new ArgumentNullException(nameof(modelPropertyExpression)); } var viewData = htmlHelper.ViewData; string propertyName = GenericExpressionHelper.GetExpressionText(modelPropertyExpression); string fullPropertyName = viewData.TemplateInfo.GetFullHtmlFieldName(propertyName); TField propertyValue = modelPropertyExpression.Compile().Invoke(viewData.Model); var partialViewData = new ViewDataDictionary(viewData); partialViewData.TemplateInfo.HtmlFieldPrefix = fullPropertyName; return(htmlHelper.PartialAsync(partialViewName, propertyValue, partialViewData)); }
private static ModelMetadata GetPropertyMetadata <TModel, TField>(IHtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TField> > modelPropertyExpression) { var modelExpression = GenericExpressionHelper.CreateModelExpression(htmlHelper.ViewData, modelPropertyExpression); return(modelExpression.Metadata); }