ViewBag initialization not possible outside of template #26

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

Comments

Projects
None yet
3 participants
Contributor

scottnickel commented Feb 17, 2012

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

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

Contributor

scottnickel commented Feb 19, 2012

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

Thanks Scott, looking forward to it.

Contributor

scottnickel commented Feb 20, 2012

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

Owner

Antaris commented Feb 21, 2012

#27

Contributor

scottnickel commented Feb 21, 2012

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)

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

Contributor

scottnickel commented Mar 13, 2012

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

Contributor

scottnickel commented Mar 13, 2012

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)

Contributor

scottnickel commented Mar 13, 2012

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)

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.

Contributor

scottnickel commented Mar 13, 2012

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)

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;
        }

    }
}

}

Contributor

scottnickel commented Mar 13, 2012

Thanks, John.

Was there an attachment or is in the repository?

Thanks,

Scott

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;
            }

        }
    }
}

Contributor

scottnickel commented Mar 13, 2012

Perfect.

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

Thanks!

Scott

Owner

Antaris commented Mar 22, 2012

Closed using pull request #36

@Antaris Antaris closed this Mar 22, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment