ASP.NET MVC Filters: How to set Viewdata for Dropdown based on action parameter

Posted by CRice on Stack Overflow See other posts from Stack Overflow or by CRice
Published on 2010-05-05T11:15:30Z Indexed on 2010/05/05 11:28 UTC
Read the original article Hit count: 197

Hi,

Im loading an entity 'Member' from its id in route data.

[ListItemsForMembershipType(true)]
public ActionResult Edit(Member someMember) {...}

The attribute on the action loads the membership type list items for a dropdown box and sticks it in viewdata. This is fine for add forms, and search forms (it gets all active items) but I need the attribute to execute BASED ON THE VALUE someMember.MembershipTypeId, because its current value must always be present when loading the item (i.e. all active items, plus the one from the loaded record).

So the question is, what is the standard pattern for this? How can my attribute accept the value or should I be loading the viewdata for the drop down in a controller supertype or during model binding or something else?

It is in an attribute now because the code to set the viewdata would otherwise be duplicated in each usage in each action.

© Stack Overflow or respective owner

Related posts about design-patterns

Related posts about asp.net-mvc