Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Html to be changed to a different model type #95

Closed
robdmoore opened this issue Aug 22, 2014 · 0 comments
Closed

Allow Html to be changed to a different model type #95

robdmoore opened this issue Aug 22, 2014 · 0 comments

Comments

@robdmoore
Copy link
Member

e.g. something like this, maybe add a BeginChameleonForm overload that takes the model type as a shorthand too...

    public static class HtmlHelperExtensions
    {
        public static HtmlHelper<TNewModel> ForModel<TNewModel>(this HtmlHelper html, TNewModel modelInstance = default(TNewModel))
        {
            var tempDataDictionary = new ViewDataDictionary(html.ViewContext.ViewData) {Model = modelInstance};
            var newViewDataDictionary = new ViewDataDictionary<TNewModel>(tempDataDictionary);
            var newViewContext = new ViewContext(
                html.ViewContext.Controller.ControllerContext,
                html.ViewContext.View,
                newViewDataDictionary,
                html.ViewContext.TempData,
                html.ViewContext.Writer
            );
            var newViewData = new ViewPage<TNewModel>
            {
                ViewContext = newViewContext,
                ViewData = newViewDataDictionary
            };
            newViewData.InitHelpers();
            return new HtmlHelper<TNewModel>(newViewContext, newViewData);
        }
    }
robdmoore added a commit that referenced this issue Oct 10, 2015
…sub-property or a random model type (with and without instance)

Partially addresses #95 and #107
robdmoore added a commit that referenced this issue Jan 15, 2016
…sub-property or a random model type (with and without instance)

Partially addresses #95 and #107
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant