Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

View Parameters and Layout
koruyucu
#1 Posted : Thursday, August 18, 2011 10:34:14 AM(UTC)
Rank: Newbie
Groups: Registered

Joined: 8/15/2011(UTC)
Posts: 8
I have hard time using views in layouts - when I call @Html.FrontHtml().RenderView("ViewName",ViewData) in layout Parameters property throws NullReference exception because PositionContext property of the Page_Context is not initialized. Is it 'by design'? If so, then how to pass parameters to view from layout?
jifeng
#2 Posted : Friday, August 19, 2011 2:45:58 PM(UTC)
Rank: Administration
Groups: Administrators, Registered

Joined: 9/3/2009(UTC)
Posts: 1,552
Location: Xiamen China
Did you got the error on the page designer? It is no way to pass parameters to the view when you using the RenderView api.
Regards,

Jifeng Huang

Kooboo Team

Microsoft ASP.NET MVP
MartinS
#3 Posted : Saturday, August 20, 2011 11:53:11 AM(UTC)
Rank: Advanced Member
Groups: Registered

Joined: 7/25/2011(UTC)
Posts: 55
Location: UK
If trying to pass variables between views, you could try storing information in Context.Items.

E.g.

Context.Items["Name"]=value;
var=Context.Items["Name"];
koruyucu
#4 Posted : Tuesday, August 23, 2011 6:26:43 AM(UTC)
Rank: Newbie
Groups: Registered

Joined: 8/15/2011(UTC)
Posts: 8
to jifeng:
Nop, the view is being rendered in the layout and because it was accessing parameters it was giving error in runtime. Anyway i solved problem by constructing and passing new ViewDataDictionary to the View and view first checks parameters, then ViewData properties. Smth like this:
var positionContext = Page_Context.Current.PositionContext;
var parameterValue = (null != positionContext ? Page_Context.Current["paramname"] : null != this.ViewData ? this.ViewData["paramname"] : null) ?? "fallback value";
ella
#5 Posted : Tuesday, March 27, 2012 11:30:44 AM(UTC)
Rank: Advanced Member
ella
Groups: Registered

Joined: 6/30/2011(UTC)
Posts: 56
Location: Russia
Example: I create a view with Parameter. I want to get this Parameter inside view plugin code. My plugin must give me some data depending on this parameter. Can I do this?
jifeng
#6 Posted : Tuesday, March 27, 2012 1:48:37 PM(UTC)
Rank: Administration
Groups: Administrators, Registered

Joined: 9/3/2009(UTC)
Posts: 1,552
Location: Xiamen China
Hi ella:

it is impossible to get the view parameter in the plugins, because the plugins is run before the view is rendered.
Regards,

Jifeng Huang

Kooboo Team

Microsoft ASP.NET MVP
ella
#7 Posted : Tuesday, March 27, 2012 2:02:45 PM(UTC)
Rank: Advanced Member
ella
Groups: Registered

Joined: 6/30/2011(UTC)
Posts: 56
Location: Russia
jifeng wrote:
it is impossible to get the view parameter in the plugins, because the plugins is run before the view is rendered.

Can you tell me how to get the view parameter in the plugin?
In view I get it like this: @Page_Context.Current["param1"], but in plugin it doesn't work.
Sorry for my stupid questions :)
jifeng
#8 Posted : Tuesday, March 27, 2012 2:21:00 PM(UTC)
Rank: Administration
Groups: Administrators, Registered

Joined: 9/3/2009(UTC)
Posts: 1,552
Location: Xiamen China
Sorry, it really impossible, because the parameter is populated when View is rendering. Please choose another way.
Regards,

Jifeng Huang

Kooboo Team

Microsoft ASP.NET MVP
ella
#9 Posted : Tuesday, March 27, 2012 3:28:25 PM(UTC)
Rank: Advanced Member
ella
Groups: Registered

Joined: 6/30/2011(UTC)
Posts: 56
Location: Russia
Oh, my mistake. I thought you wrote "it is possible"... I not attentively read you previous message... %)
It is so a shame to me, sorry. Thanks for patience.
Ethan
#10 Posted : Wednesday, May 09, 2012 12:41:45 PM(UTC)
Rank: Newbie
Groups: Registered

Joined: 2/27/2012(UTC)
Posts: 4
ella;5997 wrote:
Oh, my mistake. I thought you wrote "it is possible"... I not attentively read you previous message... %)
It is so a shame to me, sorry. Thanks for patience.


Hi Ella,
Why not add a plugin to the view? case you do that you can get view parameters in your plugin.

Page ----------> Page plugins
Layout ----------> Layout plugins
View ----------> View plugins

When I develop the view "ContactForm" I like add "ContactFormPlugin" to the view that way I can easy add the form data to my db.
ella
#11 Posted : Wednesday, May 09, 2012 3:03:54 PM(UTC)
Rank: Advanced Member
ella
Groups: Registered

Joined: 6/30/2011(UTC)
Posts: 56
Location: Russia
Ethan;6130 wrote:

Hi Ella,
Why not add a plugin to the view? case you do that you can get view parameters in your plugin.

Page ----------> Page plugins
Layout ----------> Layout plugins
View ----------> View plugins

When I develop the view "ContactForm" I like add "ContactFormPlugin" to the view that way I can easy add the form data to my db.


Oh, I'm not sure that I understand your answer, Ethan.
My question was: "how to get the view parameter in the view plugin? In view I get it like this: @Page_Context.Current["param1"], but in plugin it doesn't work."

But jifeng answered me:
jifeng;5992 wrote:
it is impossible to get the view parameter in the plugins, because the plugins is run before the view is rendered.



If you know, how to do it, please write some example code.
Ethan
#12 Posted : Thursday, May 10, 2012 6:13:42 AM(UTC)
Rank: Newbie
Groups: Registered

Joined: 2/27/2012(UTC)
Posts: 4
Hi Ella

1. Add DemoViewPlugin

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web.Mvc;
using Kooboo.CMS.Toolkit;
using Kooboo.CMS.Toolkit.Plugins;

namespace Demo.Plugins
{
public class DemoViewPlugin : PluginBase
{
public override ActionResult Execute()
{
//Incorrect
//string uuids = PageContext["uuids"];

//Correct 1
string uuids = this.PagePositionContext["uuids"].Value.AsString();

// Correct 2
//string uuids = this.PagePositionContext.GetParameterValue<string>("uuids");
return null;
}
}
}

2. Add view "DemoView"
3. Add plugin "DemoViewPlugin" to "DemoView"
1 user thanked Ethan for this useful post.
ella on 5/10/2012(UTC)
Ethan
#13 Posted : Thursday, May 10, 2012 6:14:24 AM(UTC)
Rank: Newbie
Groups: Registered

Joined: 2/27/2012(UTC)
Posts: 4
Btw add plugin to view not to page
ella
#14 Posted : Thursday, May 10, 2012 7:13:08 AM(UTC)
Rank: Advanced Member
ella
Groups: Registered

Joined: 6/30/2011(UTC)
Posts: 56
Location: Russia
Oh, greate! It's really works.
My mistake was - I used Page_Context, but I needed PagePositionContext. Thanks.

This is my code sample:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kooboo.CMS.Sites.Extension;
using Kooboo.CMS.Sites.View;
using System.Web.Mvc;

namespace Kooboo.CMS.PluginGetParam
{
    public class PluginGetParam : IPagePlugin
    {
        #region IPagePlugin Members

        public System.Web.Mvc.ActionResult Execute(Page_Context pageViewContext, PagePositionContext positionContext)
        {
            //Correct 1
            pageViewContext.ControllerContext.Controller.ViewData["sample1"] = positionContext["Group"].Value.ToString();

            // Correct 2
            pageViewContext.ControllerContext.Controller.ViewData["sample2"] = positionContext.GetParameterValue<string>("Group");

            return null;
        }

        #endregion

        public string Description
        {
            get { return "Sample plugin"; }
        }
    }
}


Why I needed this?! I want to create more usable plugin for several different pages.
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF 1.9.5.5 | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.159 seconds.