ASP.NET MVC setting menu items

View: New views
8 Messages — Rating Filter:   Alert me  

ASP.NET MVC setting menu items

by Casey Charlton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Just playing around with a new navigation we have ... and the simplest way I
can think of at the moment to "select" the current page on the menus is
something like :

[NavigationHint("INDUSTRY ANALYSIS", "Headlines")]
public ActionResult Index()
{
   return View();
}

Where the attribute then puts these values into the ViewData collection for
the navigation controls to pickup ... this does mean that individual
methods/controllers don't have to worry about looking this data up or
remembering to set it

Obviously this means the navigation is set in code rather than in
configuration ... the slightly messier option to allow some external (to
code) configuration is to put in some kind of lookup in config somewhere ...


Any other (better) ideas for cross cutting data like this?

Re: ASP.NET MVC setting menu items

by Torkel Ödegaard :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have tried some different options for doing this (one of which is
the solution you arrived at).

The one I tried last was to have typed view data, where there was a
base class for the view model that contained an enum for the active
tab.

so in the controller you would write something like
var data = new HistoryViewModel()
{
 Changes = repository.GetChanges(),
 ActiveTab = TabName.History
}
return View(data);

That is then easily tested from a controller test, the probem with
having action filters setting view data is that it makes testing it a
little more difficult.

And why is there a difference between remembering to add a
NavigationalHint attribute and remembering to set a property in the
ViewData collection?



On Wed, Aug 27, 2008 at 1:13 PM, Casey Charlton <casey@...> wrote:

> Just playing around with a new navigation we have ... and the simplest way I
> can think of at the moment to "select" the current page on the menus is
> something like :
>
> [NavigationHint("INDUSTRY ANALYSIS", "Headlines")]
> public ActionResult Index()
> {
>    return View();
> }
>
> Where the attribute then puts these values into the ViewData collection for
> the navigation controls to pickup ... this does mean that individual
> methods/controllers don't have to worry about looking this data up or
> remembering to set it
>
> Obviously this means the navigation is set in code rather than in
> configuration ... the slightly messier option to allow some external (to
> code) configuration is to put in some kind of lookup in config somewhere ...
>
>
> Any other (better) ideas for cross cutting data like this?
>
>
>

Re: ASP.NET MVC setting menu items

by Casey Charlton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Every controller method having the setting of a property in VIewData is
horrible - cross cutting concern - so the attribute removes it from the
logic of the controller method  ...

2008/8/27 Torkel Ödegaard <torkel.odegaard@...>

>   I have tried some different options for doing this (one of which is
> the solution you arrived at).
>
> The one I tried last was to have typed view data, where there was a
> base class for the view model that contained an enum for the active
> tab.
>
> so in the controller you would write something like
> var data = new HistoryViewModel()
> {
> Changes = repository.GetChanges(),
> ActiveTab = TabName.History
> }
> return View(data);
>
> That is then easily tested from a controller test, the probem with
> having action filters setting view data is that it makes testing it a
> little more difficult.
>
> And why is there a difference between remembering to add a
> NavigationalHint attribute and remembering to set a property in the
> ViewData collection?
>
>
> On Wed, Aug 27, 2008 at 1:13 PM, Casey Charlton <casey@...<casey%40goinsane.co.uk>>
> wrote:
> > Just playing around with a new navigation we have ... and the simplest
> way I
> > can think of at the moment to "select" the current page on the menus is
> > something like :
> >
> > [NavigationHint("INDUSTRY ANALYSIS", "Headlines")]
> > public ActionResult Index()
> > {
> > return View();
> > }
> >
> > Where the attribute then puts these values into the ViewData collection
> for
> > the navigation controls to pickup ... this does mean that individual
> > methods/controllers don't have to worry about looking this data up or
> > remembering to set it
> >
> > Obviously this means the navigation is set in code rather than in
> > configuration ... the slightly messier option to allow some external (to
> > code) configuration is to put in some kind of lookup in config somewhere
> ...
> >
> >
> > Any other (better) ideas for cross cutting data like this?
> >
> >
> >
>
>
>

Re: ASP.NET MVC setting menu items

by vladmagpyr :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I've gone down both routes and had more success with the Attribute
based solution

--- In altdotnet@..., "Casey Charlton" <casey@...> wrote:

>
> Every controller method having the setting of a property in VIewData is
> horrible - cross cutting concern - so the attribute removes it from the
> logic of the controller method  ...
>
> 2008/8/27 Torkel Ödegaard <torkel.odegaard@...>
>
> >   I have tried some different options for doing this (one of which is
> > the solution you arrived at).
> >
> > The one I tried last was to have typed view data, where there was a
> > base class for the view model that contained an enum for the active
> > tab.
> >
> > so in the controller you would write something like
> > var data = new HistoryViewModel()
> > {
> > Changes = repository.GetChanges(),
> > ActiveTab = TabName.History
> > }
> > return View(data);
> >
> > That is then easily tested from a controller test, the probem with
> > having action filters setting view data is that it makes testing it a
> > little more difficult.
> >
> > And why is there a difference between remembering to add a
> > NavigationalHint attribute and remembering to set a property in the
> > ViewData collection?
> >
> >
> > On Wed, Aug 27, 2008 at 1:13 PM, Casey Charlton
<casey@...<casey%40goinsane.co.uk>>
> > wrote:
> > > Just playing around with a new navigation we have ... and the
simplest
> > way I
> > > can think of at the moment to "select" the current page on the
menus is
> > > something like :
> > >
> > > [NavigationHint("INDUSTRY ANALYSIS", "Headlines")]
> > > public ActionResult Index()
> > > {
> > > return View();
> > > }
> > >
> > > Where the attribute then puts these values into the ViewData
collection
> > for
> > > the navigation controls to pickup ... this does mean that individual
> > > methods/controllers don't have to worry about looking this data
up or
> > > remembering to set it
> > >
> > > Obviously this means the navigation is set in code rather than in
> > > configuration ... the slightly messier option to allow some
external (to
> > > code) configuration is to put in some kind of lookup in config
somewhere

> > ...
> > >
> > >
> > > Any other (better) ideas for cross cutting data like this?
> > >
> > >
> > >
> >
> >
> >
>



Re: ASP.NET MVC setting menu items

by Torkel Ödegaard :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Not if each controller method needs to set a specific value :)


On Wed, Aug 27, 2008 at 4:35 PM, Casey Charlton <casey@...> wrote:

> Every controller method having the setting of a property in VIewData is
> horrible - cross cutting concern - so the attribute removes it from the
> logic of the controller method  ...
>
> 2008/8/27 Torkel Ödegaard <torkel.odegaard@...>
>>
>> I have tried some different options for doing this (one of which is
>> the solution you arrived at).
>>
>> The one I tried last was to have typed view data, where there was a
>> base class for the view model that contained an enum for the active
>> tab.
>>
>> so in the controller you would write something like
>> var data = new HistoryViewModel()
>> {
>> Changes = repository.GetChanges(),
>> ActiveTab = TabName.History
>> }
>> return View(data);
>>
>> That is then easily tested from a controller test, the probem with
>> having action filters setting view data is that it makes testing it a
>> little more difficult.
>>
>> And why is there a difference between remembering to add a
>> NavigationalHint attribute and remembering to set a property in the
>> ViewData collection?
>>
>> On Wed, Aug 27, 2008 at 1:13 PM, Casey Charlton <casey@...>
>> wrote:
>> > Just playing around with a new navigation we have ... and the simplest
>> > way I
>> > can think of at the moment to "select" the current page on the menus is
>> > something like :
>> >
>> > [NavigationHint("INDUSTRY ANALYSIS", "Headlines")]
>> > public ActionResult Index()
>> > {
>> > return View();
>> > }
>> >
>> > Where the attribute then puts these values into the ViewData collection
>> > for
>> > the navigation controls to pickup ... this does mean that individual
>> > methods/controllers don't have to worry about looking this data up or
>> > remembering to set it
>> >
>> > Obviously this means the navigation is set in code rather than in
>> > configuration ... the slightly messier option to allow some external (to
>> > code) configuration is to put in some kind of lookup in config somewhere
>> > ...
>> >
>> >
>> > Any other (better) ideas for cross cutting data like this?
>> >
>> >
>> >
>
>

Re: ASP.NET MVC setting menu items

by Torkel Ödegaard :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

But I agree menu items, depending on the application could be a cross
cutting concern and should be moved out of each controller action, but
setting the active menu item could still be handled by the controller
action.


On Wed, Aug 27, 2008 at 5:19 PM, Torkel Ödegaard
<torkel.odegaard@...> wrote:

> Not if each controller method needs to set a specific value :)
>
>
> On Wed, Aug 27, 2008 at 4:35 PM, Casey Charlton <casey@...> wrote:
>> Every controller method having the setting of a property in VIewData is
>> horrible - cross cutting concern - so the attribute removes it from the
>> logic of the controller method  ...
>>
>> 2008/8/27 Torkel Ödegaard <torkel.odegaard@...>
>>>
>>> I have tried some different options for doing this (one of which is
>>> the solution you arrived at).
>>>
>>> The one I tried last was to have typed view data, where there was a
>>> base class for the view model that contained an enum for the active
>>> tab.
>>>
>>> so in the controller you would write something like
>>> var data = new HistoryViewModel()
>>> {
>>> Changes = repository.GetChanges(),
>>> ActiveTab = TabName.History
>>> }
>>> return View(data);
>>>
>>> That is then easily tested from a controller test, the probem with
>>> having action filters setting view data is that it makes testing it a
>>> little more difficult.
>>>
>>> And why is there a difference between remembering to add a
>>> NavigationalHint attribute and remembering to set a property in the
>>> ViewData collection?
>>>
>>> On Wed, Aug 27, 2008 at 1:13 PM, Casey Charlton <casey@...>
>>> wrote:
>>> > Just playing around with a new navigation we have ... and the simplest
>>> > way I
>>> > can think of at the moment to "select" the current page on the menus is
>>> > something like :
>>> >
>>> > [NavigationHint("INDUSTRY ANALYSIS", "Headlines")]
>>> > public ActionResult Index()
>>> > {
>>> > return View();
>>> > }
>>> >
>>> > Where the attribute then puts these values into the ViewData collection
>>> > for
>>> > the navigation controls to pickup ... this does mean that individual
>>> > methods/controllers don't have to worry about looking this data up or
>>> > remembering to set it
>>> >
>>> > Obviously this means the navigation is set in code rather than in
>>> > configuration ... the slightly messier option to allow some external (to
>>> > code) configuration is to put in some kind of lookup in config somewhere
>>> > ...
>>> >
>>> >
>>> > Any other (better) ideas for cross cutting data like this?
>>> >
>>> >
>>> >
>>
>>
>

Re: ASP.NET MVC setting menu items

by Casey Charlton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

As a cross cutting concern, I certainly wouldn't want every controller  
method to manually set viewdata for properties and values outside of  
it's concern ... The attribute is probably as close as I want to get  
to that route

Anyone had better success with confit or other solutions I haven't  
thought of?

Casey Charlton
http://devlicio.us/blogs/casey


On 27 Aug 2008, at 08:19, "Torkel Ödegaard"  
<torkel.odegaard@...> wrote:

> Not if each controller method needs to set a specific value :)
>
> On Wed, Aug 27, 2008 at 4:35 PM, Casey Charlton  
> <casey@...> wrote:
> > Every controller method having the setting of a property in  
> VIewData is
> > horrible - cross cutting concern - so the attribute removes it  
> from the
> > logic of the controller method ...
> >
> > 2008/8/27 Torkel Ödegaard <torkel.odegaard@...>
> >>
> >> I have tried some different options for doing this (one of which is
> >> the solution you arrived at).
> >>
> >> The one I tried last was to have typed view data, where there was a
> >> base class for the view model that contained an enum for the active
> >> tab.
> >>
> >> so in the controller you would write something like
> >> var data = new HistoryViewModel()
> >> {
> >> Changes = repository.GetChanges(),
> >> ActiveTab = TabName.History
> >> }
> >> return View(data);
> >>
> >> That is then easily tested from a controller test, the probem with
> >> having action filters setting view data is that it makes testing  
> it a
> >> little more difficult.
> >>
> >> And why is there a difference between remembering to add a
> >> NavigationalHint attribute and remembering to set a property in the
> >> ViewData collection?
> >>
> >> On Wed, Aug 27, 2008 at 1:13 PM, Casey Charlton <casey@...
> >
> >> wrote:
> >> > Just playing around with a new navigation we have ... and the  
> simplest
> >> > way I
> >> > can think of at the moment to "select" the current page on the  
> menus is
> >> > something like :
> >> >
> >> > [NavigationHint("INDUSTRY ANALYSIS", "Headlines")]
> >> > public ActionResult Index()
> >> > {
> >> > return View();
> >> > }
> >> >
> >> > Where the attribute then puts these values into the ViewData  
> collection
> >> > for
> >> > the navigation controls to pickup ... this does mean that  
> individual
> >> > methods/controllers don't have to worry about looking this data  
> up or
> >> > remembering to set it
> >> >
> >> > Obviously this means the navigation is set in code rather than in
> >> > configuration ... the slightly messier option to allow some  
> external (to
> >> > code) configuration is to put in some kind of lookup in config  
> somewhere
> >> > ...
> >> >
> >> >
> >> > Any other (better) ideas for cross cutting data like this?
> >> >
> >> >
> >> >
> >
> >
>

Re: ASP.NET MVC setting menu items

by Ben Scheirman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

For this I used a view helper... something like:

Html.ActionLink(text, controller, action, classIfCurrent, classIfNot)

It was working for preview 2 on codecampserver and then stopped working... I
need to go back and update it.

On Wed, Aug 27, 2008 at 7:05 PM, Casey Charlton <casey@...>wrote:

>  As a cross cutting concern, I certainly wouldn't want every controller
> method to manually set viewdata for properties and values outside of it's
> concern ... The attribute is probably as close as I want to get to that
> route
>
> Anyone had better success with confit or other solutions I haven't thought
> of?
>
> Casey Charltonhttp://devlicio.us/blogs/casey
>
>
> On 27 Aug 2008, at 08:19, "Torkel Ödegaard" <torkel.odegaard@...>
> wrote:
>
>  Not if each controller method needs to set a specific value :)
>
> On Wed, Aug 27, 2008 at 4:35 PM, Casey Charlton <casey@...<casey%40goinsane.co.uk>>
> wrote:
> > Every controller method having the setting of a property in VIewData is
> > horrible - cross cutting concern - so the attribute removes it from the
> > logic of the controller method ...
> >
> > 2008/8/27 Torkel Ödegaard <torkel.odegaard@...<torkel.odegaard%40gmail.com>
> >
> >>
> >> I have tried some different options for doing this (one of which is
> >> the solution you arrived at).
> >>
> >> The one I tried last was to have typed view data, where there was a
> >> base class for the view model that contained an enum for the active
> >> tab.
> >>
> >> so in the controller you would write something like
> >> var data = new HistoryViewModel()
> >> {
> >> Changes = repository.GetChanges(),
> >> ActiveTab = TabName.History
> >> }
> >> return View(data);
> >>
> >> That is then easily tested from a controller test, the probem with
> >> having action filters setting view data is that it makes testing it a
> >> little more difficult.
> >>
> >> And why is there a difference between remembering to add a
> >> NavigationalHint attribute and remembering to set a property in the
> >> ViewData collection?
> >>
> >> On Wed, Aug 27, 2008 at 1:13 PM, Casey Charlton <casey@...<casey%40goinsane.co.uk>
> >
> >> wrote:
> >> > Just playing around with a new navigation we have ... and the simplest
> >> > way I
> >> > can think of at the moment to "select" the current page on the menus
> is
> >> > something like :
> >> >
> >> > [NavigationHint("INDUSTRY ANALYSIS", "Headlines")]
> >> > public ActionResult Index()
> >> > {
> >> > return View();
> >> > }
> >> >
> >> > Where the attribute then puts these values into the ViewData
> collection
> >> > for
> >> > the navigation controls to pickup ... this does mean that individual
> >> > methods/controllers don't have to worry about looking this data up or
> >> > remembering to set it
> >> >
> >> > Obviously this means the navigation is set in code rather than in
> >> > configuration ... the slightly messier option to allow some external
> (to
> >> > code) configuration is to put in some kind of lookup in config
> somewhere
> >> > ...
> >> >
> >> >
> >> > Any other (better) ideas for cross cutting data like this?
> >> >
> >> >
> >> >
> >
> >
>
>
>