tags:

views:

44

answers:

2

not sure if this is possible or if i am down the wrong road.

i am wanting to use an attribute to clean out single quotes from my form posted data (this will be changed, but single quotes is a good example)

i have created the actionFilter as below:

public class RemoveSingleQuotesAttribute : ActionFilterAttribute
{
    public NameValueCollection collection { get; set; }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        for (int i = 0; i < collection.Count; i++)
        {
            collection.Set(
                collection.GetKey(i),
                collection.GetValues(i).ToString().Replace("'","`")
                );
        }
    }
}

now this is where i get stuck:

when i type [RemoveSingleQuotes()] in brackets i only get [Int Order] as the intelisense and not formcollection/Namevaluecollection

and also how do i pass a collection anyway ?????

is this even possible or am i just creating some mad sh*t up here ????

thanks

+1  A: 

You can access formvalues in the filter by filterContext.RequestContext.HttpContext.Request.Form. Anyway it would be easier to build a custom modelbinder that strips of unwanted characters.

That way you can still work with the a bound Model. Your solution will lead to a changed Formcollection, but an unchanged Model. You could call UpdateModel after your changes, but it is still is not a good solution.

EDIT

public class CustomModelBinder :  DefaultModelBinder {
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) {
    if(propertyDescriptor.Name == "PropertyWithCharactersIneedToReplace") {
        MethodThatReplacesCharacters();
        return;
    }

    base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
}

EDIT 1 I would apply the ModelBinder this way:

public ActionResult UpdateSomeObject([ModelBinder(typeof(NiceModelBinder))]SomeViewModelType model)
Malcolm Frexner
my model is subsonic, and i update the .tt files every time a new bug or feature is added... to go your route i would have to alter my TT files every time i wanted my model to do the work i want it to carry out.... so i figure a filter i can write once to do all the procedures i would carry out on a string. i was hopeing here the filter is executed first, so then my formcollection IS then ready to bind as this comes afterwords right ???? unless i have the wrong end of the stick, then please elaborate? thanks for your comments
minus4
have been thinking the custom model idea is good i see where your going... however is this better? i would have to create a model binder for each class i have and would have to keep remebering to build a new one everytime i get a new table, if this was to happen! and would a default modelbinder overwrite any subsonic model binders ????
minus4
Hopefully you can work with one Modelbinder for all your classes.I would try to stay with the Defaultbinder and replace only some parts. The main point is realy where you work: In BindModel or BinProperty. See the edited post.
Malcolm Frexner
hmmmmmmmmmm nice, but the reason i said about seperate for each is that you have to declare a model binder for typeof within your global.asax like so: ModelBinders.Binders[typeof(Customer)] = new CustomerBinder();so if you have the patience still! how would you instantiate the above code?
minus4
I would set the ModelBinder at the action. (see edited code) There is no way I know to apply a ModelBinder to all types.
Malcolm Frexner
A: 

Why don't you work with filterContext.HttpContext.Request.Form instead of passing it to the filter? As far as I understand this is the collection that you need.

queen3
yeah i did try to pull httpcontext.current.request.form, but i think i had no .form available but will try again., in this case though do i just pass nothing to the filter ????
minus4