Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC 2, IModelBinder & ValueProvider changes
    primarykey
    data
    text
    <p>I'm trying to migrate to ASP.Net MVC 2 and meet some issues. Here is one : I needed to bind directly a <strong>Dictionary</strong> as result of a view post.</p> <p>In ASP.Net MVC 1 it worked perfectly using a custom <strong>IModelBinder</strong> :</p> <pre><code>/// &lt;summary&gt; /// Bind Dictionary&lt;int, int&gt; /// /// convention : &lt;elm name="modelName_key" value="value"&gt;&lt;/elm&gt; /// &lt;/summary&gt; public class DictionaryModelBinder : IModelBinder { #region IModelBinder Members /// &lt;summary&gt; /// Mandatory /// &lt;/summary&gt; public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { IDictionary&lt;int, int&gt; retour = new Dictionary&lt;int, int&gt;(); // get the values var values = bindingContext.ValueProvider; // get the model name string modelname = bindingContext.ModelName + '_'; int skip = modelname.Length; // loop on the keys foreach(string keyStr in values.Keys) { // if an element has been identified if(keyStr.StartsWith(modelname)) { // get that key 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 } </code></pre> <p>It worked in pair with some smart HtmlBuilder that displayed dictionary of data.</p> <p>The problem I meet now is that <strong>ValueProvider</strong> is not a Dictionary&lt;> anymore, it's a IValueProvider that only allow to get values whose name is known</p> <pre><code>public interface IValueProvider { bool ContainsPrefix(string prefix); ValueProviderResult GetValue(string key); } </code></pre> <p>This is really not cool as I cannot perform my smart parsing...</p> <p>Question :</p> <ol> <li>Is there another way to get all keys ?</li> <li>Do you know another way to bind a collection of HTML elements to a Dictionary</li> </ol> <p>Thanks for your suggestions</p> <p>O.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload