Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 2 - Setting values on IValueProvider

I am attempting to upgrade my MVC 1 project to MVC 2 RC. We currently have a custom modelbinder that adds items to the ValueProvider (this worked when it was a dictionary). We then passed this off to the default modelbinder. However, IValueProvider does not have an add method, so this algorithm no longer works. Does anyone know of a way to add values to the ValueProvider in MVC 2?

foreach(string valKey in controllerContext.RequestContext.HttpContext.Request.Form.AllKeys.Where(x => x.StartsWith(valuesToChangePrefix)))
{
    string valName = valKey.Substring(valuesToChangePrefix.Length);

    string myVal = ManipulateValue(bindingContext.ValueProvider.GetValue(valKey).AttemptedValue);

    // This is where I need to add to my value Provider (As you can see I used to just assign a ValueProviderResult
    //bindingContext.ValueProvider = new ValueProviderResult(myVal.Split(','), myVal, bindingContext.ValueProvider.GetValue(valKey).Culture);
}
like image 525
Keith Rousseau Avatar asked Dec 24 '09 16:12

Keith Rousseau


1 Answers

The problem is that by the time you get to the ModelBinder, your ValueProvider has already been set. Previously, you could add values to your ValueProvider at this point.

In order to provide a Custom ValueProvider, you need to override the ControllerActionInvoker, which is the ultimate solution. Unfortunately the ControllerActionInvoker is created by the Controller object, instead of being injected. Therefore, you also need to override Controller to call your own ControllerActionInvoker.

like image 183
Keith Rousseau Avatar answered Nov 03 '22 18:11

Keith Rousseau