I am trying to upgrade to ASP.Net MVC 2 and solve some problems. Here is one of them: I needed to attach the Dictionary directly as a result of the viewing message.
In ASP.Net MVC 1, it worked perfectly using custom IModelBinder :
public class DictionaryModelBinder : IModelBinder
{
#region IModelBinder Members
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
IDictionary<int, int> retour = new Dictionary<int, int>();
var values = bindingContext.ValueProvider;
string modelname = bindingContext.ModelName + '_';
int skip = modelname.Length;
foreach(string keyStr in values.Keys)
{
if(keyStr.StartsWith(modelname))
{
int key;
if(Int32.TryParse(keyStr.Substring(skip), out key))
{
int value;
if(Int32.TryParse(values[keyStr].AttemptedValue, out value))
retour.Add(key, value);
}
}
}
return retour;
}
#endregion
}
He was paired with some intelligent HtmlBuilder that displayed a data dictionary.
The problem that I am facing now is that ValueProvider is no longer a dictionary <>, it is IValueProvider, which allows you to get values whose name is known
public interface IValueProvider
{
bool ContainsPrefix(string prefix);
ValueProviderResult GetValue(string key);
}
This is really not cool, as I cannot do my intellectual parsing ...
Question:
.