Skip to content

Instantly share code, notes, and snippets.

@DavidRogersDev
Created October 7, 2017 03:46
Show Gist options
  • Save DavidRogersDev/4e9725ecc55fc72bdaaac1c147a22b4e to your computer and use it in GitHub Desktop.
Save DavidRogersDev/4e9725ecc55fc72bdaaac1c147a22b4e to your computer and use it in GitHub Desktop.
Extension method CreateViewDataDictionary to create a new ViewDataDictionary in ASP.NET Core
public static ViewDataDictionary<T> CreateViewDataDictionary<T>(this HttpContext httpContext, T model)
{
var modelMetadataProvider = httpContext.ApplicationServices.GetRequiredService<IModelMetadataProvider>();
return new ViewDataDictionary<T>(modelMetadataProvider, new ModelStateDictionary())
{
Model = model
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment