private static void FakeModel(ViewContentHelper content, ContentRegistration re) { if (content.Html.ViewContext.ViewData.Model == null) { var modelType = content.Html.GetType().GetGenericArguments()[0]; object model; if (modelType.IsAssignableFrom(re.ContentType) && !re.ContentType.IsAbstract) { model = Activator.CreateInstance(re.ContentType); } else if (typeof(ContentItem).IsAssignableFrom(modelType) && !modelType.IsAbstract) { model = Activator.CreateInstance(modelType); } else if (modelType.IsGenericType && typeof(IEnumerable <>).IsAssignableFrom(modelType.GetGenericTypeDefinition())) { model = Array.CreateInstance(modelType.GetGenericTypeDefinition().MakeArrayType(), 0); } else { return; } content.Html.ViewContext.ViewData.Model = content.Html.ViewData.Model = model; } }
public static ContentRegistration Define(this ViewContentHelper content, Action <ContentRegistration> registration) { var re = RegistrationExtensions.GetRegistrationExpression(content.Html); if (re != null) { FakeModel(content, re); re.Context.GlobalSortOffset = 0; if (content.Html.GetType().IsGenericType) { var contentType = content.Html.GetType().GetGenericArguments().First(); if (typeof(ContentItem).IsAssignableFrom(contentType) && re.ContentType == null) { re.ContentType = contentType; re.Title = contentType.Name; } } re.IsDefined = true; if (registration != null) { registration(re); } } return(re); }
public static ContentRegistration PrependDefinition(this ViewContentHelper content, Action <ContentRegistration> registration = null) { var re = RegistrationExtensions.GetRegistrationExpression(content.Html); if (re != null) { re.Context.GlobalSortOffset = -1000; registration(re); } return(re); }
// content helper public static ViewContentHelper Content(this HtmlHelper html) { string key = "ContentHelperOf" + html.GetHashCode(); var content = html.ViewContext.ViewData[key] as ViewContentHelper; if (content == null) { html.ViewContext.ViewData[key] = content = new ViewContentHelper(html); } return(content); }
public static ContentRegistration PrependDefinition(this ViewContentHelper content) { return(content.PrependDefinition(null)); }
public static ContentRegistration Define(this ViewContentHelper content) { return(content.Define(null)); }