Viewdata in Application.spark

0 views
Skip to first unread message

xteron

unread,
Sep 4, 2010, 4:48:28 AM9/4/10
to Spark View Engine Dev
Hello,

I have written this in the mode/menu.cs file:

IQueryable<MenuItem> ViewMenu()
{
return from ost in db.MenuItems
orderby ost.SortID
select ost;
}


I want to use this in the application.spark file to view the menu on
all pages? What is the best way to do that?

-xteron

TMFReason

unread,
Sep 7, 2010, 11:31:22 AM9/7/10
to Spark View Engine Dev
I'm not sure of the best way. But the way I might use would be to
(using .Net 3.5):

1) Make a MenuItemViewMode.cs with needed properties like Text and
Link (or just use your MenuItem if just a DTO).
2) Add a BuildMenuList.cs filter that in OnActionExecuting does your
query, maps each MenuItem to MenuItemViewModel, and sets
filterContext.Controller.ViewData["MenuItems"] =
IEnumerable<MenuItemViewModel>.
3) Put this filter on the Controller(s) as an attribute
[BuildMenuList].
4) Add a partial view <MenuItems/> to the master layout in
application.spark.
5) Have _MenuItems.spark use <li each='var item in
ViewData["MenuItems"]'><a href="!{item.Link}">${item.Text}</a></li>.

The best way would probably not use a filter, add the list to a
strongly typed model, and do some caching.

Sean Epping

unread,
Sep 4, 2010, 9:18:19 AM9/4/10
to spar...@googlegroups.com
I have not used IQueryable, but I know IEnumerable or IList will work.  You just add a viewdata tag to the top of the view as such:

<viewdata model='IList[[MenuItem]]' />

you can then access the model property as in webforms view engine.  There is much more documentation on it at the spark website.

Hope this helps.


--
You received this message because you are subscribed to the Google Groups "Spark View Engine Dev" group.
To post to this group, send email to spar...@googlegroups.com.
To unsubscribe from this group, send email to spark-dev+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spark-dev?hl=en.


xteron

unread,
Sep 16, 2010, 12:58:37 PM9/16/10
to Spark View Engine Dev
Thank you so much. Will try what you sad and see if I get it to
work. :)

-xteron

On Sep 4, 3:18 pm, Sean Epping <epp...@gmail.com> wrote:
> I have not used IQueryable, but I know IEnumerable or IList will work.  You
> just add a viewdata tag to the top of the view as such:
>
> <viewdata model='IList[[MenuItem]]' />
>
> you can then access the model property as in webforms view engine.  There is
> much more documentation on it at the spark website.
>
> Hope this helps.
>
> On Sat, Sep 4, 2010 at 3:48 AM, xteron <thronda...@gmail.com> wrote:
> > Hello,
>
> > I have written this in the mode/menu.cs file:
>
> > IQueryable<MenuItem> ViewMenu()
> >        {
> >            return from ost in db.MenuItems
> >                   orderby ost.SortID
> >                   select ost;
> >        }
>
> > I want to use this in the application.spark file to view the menu on
> > all pages? What is the best way to do that?
>
> > -xteron
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Spark View Engine Dev" group.
> > To post to this group, send email to spar...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > spark-dev+...@googlegroups.com<spark-dev%2Bunsu...@googlegroups.com>
> > .
Reply all
Reply to author
Forward
0 new messages