views:

134

answers:

1

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.

+2  A: 

You can access the model object inside MVC filter OnActionExecuting:

public class ListItemsForMembershipType : ActionFilterAttribute
{
    // ...

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);
        Member model = filterContext.ActionParameters["someMember"] as Member
        // do your stuff, insert data into ViewData            
    }
}
PanJanek