Last active
June 7, 2017 19:17
-
-
Save jeremysimmons/16548c6ee49f621c734eddda6dfe433d to your computer and use it in GitHub Desktop.
Html.Partial for asp.net core mvc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class HtmlHelperPartialExtensions | |
{ | |
public static IHtmlContent PartialWithPrefix(this IHtmlHelper htmlHelper, string partialViewName, object model, string prefix) | |
{ | |
var htmlFieldPrefix = (string.Empty.Equals(prefix) ? "." : "") + prefix; | |
return htmlHelper.Partial(partialViewName, model, new ViewDataDictionary(htmlHelper.ViewData) { TemplateInfo = { HtmlFieldPrefix = htmlFieldPrefix } }); | |
} | |
public static Task<IHtmlContent> PartialWithPrefixAsync(this IHtmlHelper htmlHelper, string partialViewName, object model, string prefix) | |
{ | |
var htmlFieldPrefix = (string.Empty.Equals(prefix) ? "." : "") + prefix; | |
return htmlHelper.PartialAsync(partialViewName, model, new ViewDataDictionary(htmlHelper.ViewData) {TemplateInfo = {HtmlFieldPrefix = htmlFieldPrefix}}); | |
} | |
public static IHtmlContent PartialWithPrefixFor<TModel, TProperty>(this IHtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, string partialViewName) | |
{ | |
string prefix = ExpressionHelper.GetExpressionText(expression); | |
object model = ExpressionMetadataProvider.FromLambdaExpression(expression, helper.ViewData, helper.MetadataProvider).Model; | |
return PartialWithPrefix(helper, partialViewName, model, prefix); | |
} | |
public static Task<IHtmlContent> PartialWithPrefixForAsync<TModel, TProperty>(this IHtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, string partialViewName) | |
{ | |
string prefix = ExpressionHelper.GetExpressionText(expression); | |
object model = ExpressionMetadataProvider.FromLambdaExpression(expression, helper.ViewData, helper.MetadataProvider).Model; | |
return PartialWithPrefixAsync(helper, partialViewName, model, prefix); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment