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

ViewBag initialization not possible outside of template #26

Closed
scottnickel opened this issue Feb 17, 2012 · 17 comments
Closed

ViewBag initialization not possible outside of template #26

scottnickel opened this issue Feb 17, 2012 · 17 comments
Assignees

Comments

@scottnickel
Copy link
Contributor

The Parse/Run APIs do not provide a way to pass in ViewBag contents.

Only ViewBag properties set/get from the page will work.

I have extended the interfaces/classes with overloads to provide this functionality.

This will allow an application to initialize ViewBag contents before executing the template and the template @ViewBag will contain the initialized values.

Let me know if you would like me to upload those changes.

Scott

@johnwebbcole
Copy link

Scott,
I'd be interested in seeing your changes. I have an implementation with 2.1 that has viewbag, @html and @url helpers working, but not one with 3.0 yet.

Thanks,

John

@scottnickel
Copy link
Contributor Author

Hi John.

No problem at all.

The changes are minor.

I will ZIP them up and send them to you once I comment them.

I will get this done today.

Thanks,

Scott

-----Original Message-----
From: johnwebbcole [mailto:reply@reply.github.com]
Sent: Saturday, February 18, 2012 11:22 AM
To: Scott Nickel
Subject: Re: [RazorEngine] ViewBag contents set in the controller cannot be used in view (#26)

Scott,
I'd be interested in seeing your changes. I have an implementation with 2.1 that has viewbag, @html and @url helpers working, but not one with 3.0 yet.

Thanks,

John


Reply to this email directly or view it on GitHub:
#26 (comment)

@ghost ghost assigned Antaris Feb 19, 2012
@johnwebbcole
Copy link

Thanks Scott, looking forward to it.

@scottnickel
Copy link
Contributor Author

Hi John.

Two things have delayed me.

  1. I ran the unit tests and something is wrong with Dynamic objects (which I am debugging as we speak).

  2. I spoke with Matt (one of the leads on this) and he wants me to create a branch and put my stuff out there.

I can ZIP and email you directly when I am done so you don't have to wait for the branch.

I will email you when I have this 100% working via unit tests.

Thanks,

Scott

@Antaris
Copy link
Owner

Antaris commented Feb 21, 2012

#27

@scottnickel
Copy link
Contributor Author

Nice...thanks!

Scott

-----Original Message-----
From: Matthew Abbott [mailto:reply@reply.github.com]
Sent: Tuesday, February 21, 2012 5:43 AM
To: Scott Nickel
Subject: Re: [RazorEngine] ViewBag contents set in the controller cannot be used in view (#26)

#27


Reply to this email directly or view it on GitHub:
#26 (comment)

@johnwebbcole
Copy link

Scott,
Thanks for the update. I think a branch is a great idea, it will make it easier to keep track of and expand upon. And before that, I'll look for your zip file.

John

@scottnickel
Copy link
Contributor Author

I have forked and pushed changes to this issue.

git@github.com:scottnickel/RazorEngine.git

Tag: 20120313_Issue26_Issue27

A pull request is pending.

Scott

@scottnickel
Copy link
Contributor Author

Hi John.

I FINALLY got my changes up there.

I have forked and pushed changes to this issue.

git@github.com:scottnickel/RazorEngine.git

Tag: 20120313_Issue26_Issue27

A pull request is pending.

Have a look and let me know if you find any problems.

Thanks,

Scott

-----Original Message-----
From: Scott Nickel
Sent: Sunday, February 19, 2012 1:04 PM
To: 'johnwebbcole'
Subject: RE: [RazorEngine] ViewBag contents set in the controller cannot be used in view (#26)

Hi John.

No problem at all.

The changes are minor.

I will ZIP them up and send them to you once I comment them.

I will get this done today.

Thanks,

Scott

-----Original Message-----
From: johnwebbcole [mailto:reply@reply.github.com]
Sent: Saturday, February 18, 2012 11:22 AM
To: Scott Nickel
Subject: Re: [RazorEngine] ViewBag contents set in the controller cannot be used in view (#26)

Scott,
I'd be interested in seeing your changes. I have an implementation with 2.1 that has viewbag, @html and @url helpers working, but not one with 3.0 yet.

Thanks,

John


Reply to this email directly or view it on GitHub:
#26 (comment)

@scottnickel
Copy link
Contributor Author

Hi John.

Can you send me the code where you create the HtmlHelper object?

Thanks,

Scott

-----Original Message-----
From: johnwebbcole [mailto:reply@reply.github.com]
Sent: Saturday, February 18, 2012 11:22 AM
To: Scott Nickel
Subject: Re: [RazorEngine] ViewBag contents set in the controller cannot be used in view (#26)

Scott,
I'd be interested in seeing your changes. I have an implementation with 2.1 that has viewbag, @html and @url helpers working, but not one with 3.0 yet.

Thanks,

John


Reply to this email directly or view it on GitHub:
#26 (comment)

@Antaris
Copy link
Owner

Antaris commented Mar 13, 2012

I've got an implementation of @html and @url in the MvcTemplateBase<T> type which I am hoping to push to Github this week. There are some caveats in my implementation. To correctly provide context to the Html and Url calls, e.g. @Html.Partial("_LogOnPartial"), @Url.Action("Index"), etc., a valid ControllerContext must be created, which does mean the MvcTemplateBase template types will be tied heaviliy into ASP.NET MVC. You won't be able to use these in a non-MVC application. If I mock the ControllerContext, and associated types, RequestContext, RouteData, etc., I fear the Html and Url instances would be too error prone/useless.

Will push soon I hope.

@scottnickel
Copy link
Contributor Author

That is what I am looking for.

I have an MVC ViewEngine and View.

The UrlHelper is easy and can be instantiated using information from HttpContext.

My issue is: How does MvcTemplateBase() know what the current View & Controller is, so it can instantiate its HtmlHelper object internally when RazorEngine processes the ITemplate. I poked around HttpContext.Current and haven't found what I need. Any thoughts?

If you want, send me what you have before push/pull and I will make sure it meets my needs.

Thanks,

Scott

-----Original Message-----
From: Matthew Abbott [mailto:reply@reply.github.com]
Sent: Tuesday, March 13, 2012 12:43 PM
To: Scott Nickel
Subject: Re: [RazorEngine] ViewBag initialization not possible outside of template (#26)

I've got an implementation of @html and @url in the MvcTemplateBase<T> type which I am hoping to push to Github this week. There are some caveats in my implementation. To correctly provide context to the Html and Url calls, e.g. @Html.Partial("_LogOnPartial"), @Url.Action("Index"), etc., a valid ControllerContext must be created, which does mean the MvcTemplateBase template types will be tied heaviliy into ASP.NET MVC. You won't be able to use these in a non-MVC application. If I mock the ControllerContext, and associated types, RequestContext, RouteData, etc., I fear the Html and Url instances would be too error prone/useless.

Will push soon I hope.


Reply to this email directly or view it on GitHub:
#26 (comment)

@johnwebbcole
Copy link

Scott,
Here is the HtmlBase that we are using right now with 3.0.8beta and the MvcTemplateBase version as well.

John


From: Scott Nickel reply@reply.github.com
To: johnwebbcole bitwise17-github@yahoo.com
Sent: Tuesday, March 13, 2012 11:30 AM
Subject: Re: [RazorEngine] ViewBag initialization not possible outside of template (#26)

Hi John.

Can you send me the code where you create the HtmlHelper object?

Thanks,

Scott

-----Original Message-----
From: johnwebbcole [mailto:reply@reply.github.com]
Sent: Saturday, February 18, 2012 11:22 AM
To: Scott Nickel
Subject: Re: [RazorEngine] ViewBag contents set in the controller cannot be used in view (#26)

Scott,
I'd be interested in seeing your changes. I have an implementation with 2.1 that has viewbag, @html and @url helpers working, but not one with 3.0 yet.

Thanks,

John


Reply to this email directly or view it on GitHub:
#26 (comment)


Reply to this email directly or view it on GitHub:
#26 (comment)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RazorEngine;
using RazorEngine.Templating;
using System.Web.Mvc;
using System.Web.WebPages;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace System.Web.Mvc

{

[RequireNamespaces("System.Web.Mvc.Html")]
public class HtmlTemplateBase<T>:TemplateBase<T>, IViewDataContainer
{
    private HtmlHelper<T> helper = null;
    private ViewDataDictionary viewdata = null;
    private System.Dynamic.DynamicObject viewbag = null;
    private UrlHelper urlhelper = null;

    public dynamic ViewBag
    {
        get
        {
            return (WebPageContext.Current.Page as WebViewPage).ViewBag;
        }
    }

    public HtmlHelper<T> Html
    {
        get
        {
            if (helper == null)
            {
                var p = WebPageContext.Current;
                var wvp = p.Page as WebViewPage;
                var context = wvp != null ? wvp.ViewContext : null;
                helper = new HtmlHelper<T>(context, this, RouteTable.Routes);
            }
            return helper;
        }
    }

    public UrlHelper Url
    {
        get
        {
            if (urlhelper == null)
            {
                urlhelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
            }
            return urlhelper;
        }
    }

    public ViewDataDictionary ViewData
    {
        get
        {
            if (viewbag == null)
            {
                var p = WebPageContext.Current;
                var viewcontainer = p.Page as IViewDataContainer;
                viewdata = new ViewDataDictionary(viewcontainer.ViewData);

                if (this.Model != null)
                {
                    viewdata.Model = Model;
                }

            }

            return viewdata;
        }
        set
        {
            viewdata = value;
        }

    }

}

}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RazorEngine;
using RazorEngine.Templating;
using System.Web.Mvc;
using System.Web.WebPages;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace RazorEngine.Mvc
{
class MvcTemplateBase : TemplateBase, IViewDataContainer
{
private HtmlHelper helper = null;
private ViewDataDictionary viewdata = null;
private System.Dynamic.DynamicObject viewbag = null;
private UrlHelper urlhelper = null;

    //public dynamic ViewBag
    //{
    //    get
    //    {
    //        return (WebPageContext.Current.Page as WebViewPage).ViewBag;
    //    }
    //}

    public HtmlHelper<T> Html
    {
        get
        {
            if (helper == null)
            {
                var p = WebPageContext.Current;
                var wvp = p.Page as WebViewPage;
                var context = wvp != null ? wvp.ViewContext : null;
                helper = new HtmlHelper<T>(context, this, RouteTable.Routes);
            }
            return helper;
        }
    }

    public UrlHelper Url
    {
        get
        {
            if (urlhelper == null)
            {
                urlhelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
            }
            return urlhelper;
        }
    }

    public ViewDataDictionary ViewData
    {
        get
        {
            if (viewbag == null)
            {
                var p = WebPageContext.Current;
                var viewcontainer = p.Page as IViewDataContainer;
                viewdata = new ViewDataDictionary(viewcontainer.ViewData);

                if (this.Model != null)
                {
                    viewdata.Model = Model;
                }

            }

            return viewdata;
        }
        set
        {
            viewdata = value;
        }

    }
}

}

@scottnickel
Copy link
Contributor Author

Thanks, John.

Was there an attachment or is in the repository?

Thanks,

Scott

@johnwebbcole
Copy link

Intended to have an attachment... I'll inline it here.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RazorEngine;
using RazorEngine.Templating;
using System.Web.Mvc;
using System.Web.WebPages;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace System.Web.Mvc

{

    [RequireNamespaces("System.Web.Mvc.Html")]
    public class HtmlTemplateBase<T>:TemplateBase<T>, IViewDataContainer
    {
        private HtmlHelper<T> helper = null;
        private ViewDataDictionary viewdata = null;
        private System.Dynamic.DynamicObject viewbag = null;
        private UrlHelper urlhelper = null;

        public dynamic ViewBag
        {
            get
            {
                return (WebPageContext.Current.Page as WebViewPage).ViewBag;
            }
        }

        public HtmlHelper<T> Html
        {
            get
            {
                if (helper == null)
                {
                    var p = WebPageContext.Current;
                    var wvp = p.Page as WebViewPage;
                    var context = wvp != null ? wvp.ViewContext : null;
                    helper = new HtmlHelper<T>(context, this, RouteTable.Routes);
                }
                return helper;
            }
        }

        public UrlHelper Url
        {
            get
            {
                if (urlhelper == null)
                {
                    urlhelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
                }
                return urlhelper;
            }
        }

        public ViewDataDictionary ViewData
        {
            get
            {
                if (viewbag == null)
                {
                    var p = WebPageContext.Current;
                    var viewcontainer = p.Page as IViewDataContainer;
                    viewdata = new ViewDataDictionary(viewcontainer.ViewData);

                    if (this.Model != null)
                    {
                        viewdata.Model = Model;
                    }

                }

                return viewdata;
            }
            set
            {
                viewdata = value;
            }

        }

    }



}

MVCTemplateBaseOfT

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RazorEngine;
using RazorEngine.Templating;
using System.Web.Mvc;
using System.Web.WebPages;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace RazorEngine.Mvc
{
    class MvcTemplateBase<T> : TemplateBase<T>, IViewDataContainer
    {
        private HtmlHelper<T> helper = null;
        private ViewDataDictionary viewdata = null;
        private System.Dynamic.DynamicObject viewbag = null;
        private UrlHelper urlhelper = null;

        //public dynamic ViewBag
        //{
        //    get
        //    {
        //        return (WebPageContext.Current.Page as WebViewPage).ViewBag;
        //    }
        //}

        public HtmlHelper<T> Html
        {
            get
            {
                if (helper == null)
                {
                    var p = WebPageContext.Current;
                    var wvp = p.Page as WebViewPage;
                    var context = wvp != null ? wvp.ViewContext : null;
                    helper = new HtmlHelper<T>(context, this, RouteTable.Routes);
                }
                return helper;
            }
        }

        public UrlHelper Url
        {
            get
            {
                if (urlhelper == null)
                {
                    urlhelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
                }
                return urlhelper;
            }
        }

        public ViewDataDictionary ViewData
        {
            get
            {
                if (viewbag == null)
                {
                    var p = WebPageContext.Current;
                    var viewcontainer = p.Page as IViewDataContainer;
                    viewdata = new ViewDataDictionary(viewcontainer.ViewData);

                    if (this.Model != null)
                    {
                        viewdata.Model = Model;
                    }

                }

                return viewdata;
            }
            set
            {
                viewdata = value;
            }

        }
    }
}

@scottnickel
Copy link
Contributor Author

Perfect.

The old WebPageContext.Current.....just what I was looking for.

Thanks!

Scott

@Antaris
Copy link
Owner

Antaris commented Mar 22, 2012

Closed using pull request #36

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants