This project is read-only.

IEnumerable Conversion and Transformations chaining

 Most of helpers that operates on collections such as SortableListFor or DataGridFor are able to operate only on List<T>,  but the ConvertToList helper is able to convert any IEnumerable that either is an Array or that conforms to the following conditions:

  • It has an Add or AddItem method
  • It has a constructor with no parameters or with a a single initial lenght parameter.

When the page is posted back the IEnumerable is converted back to its initial type.  Suppose now that the keywords in the SortableListFor example are stored into an Array, the code becomes:


@{ RenderInfo<List<string>> convertedKeywords =
       Html.ConvertToList<RegisterModel, string[], string>(m => m.Keywords); }
 Func<HtmlHelper<string>, string> keywordsTemplate = 
      (x) => x.TypedTextBoxFor(m => m, watermarkCss:"watermark", overrideWatermark: "insert keyword").ToString()+
      x.SortableListDeleteButton("Delete",  ManipulationButtonStyle.Link).ToString();
       (x) => x.TypedTextBoxFor(m => m, watermarkCss: "watermark", overrideWatermark: "insert keyword").ToString() +
              x.SortableListDeleteButton("Delete",  ManipulationButtonStyle.Link).ToString()
   "KeywordInsert", 0.8f,
   htmlAttributesContainer: new Dictionary<string, object> { { "class", "SortableList" } },
                               itemCss: "normalRow", altItemCss: "alternateRow")
@Html.SortableListAddButtonFor(convertedKeywords, "Add New Item")



The RenderInfo<T> class is the standard format used to chain transformation operations in the MVCControlToolkit. Most of MVCControlToolkit helpers accept both Lambda Expressions or RenderInfo<T> to specify the data they have to operate on.  The RenderInfo<T> format enables them to accept as input the output of other helpers.

Often, when taking the input from the output of a previous transformation, one needs to extract just a part of the output. In such cases one can use the Extract helper:


public static  RenderInfo<NM> Extract<VM, OM, NM>(
            this HtmlHelper<VM> htmlHelper,
            Expression<Func<OM, NM>> expression,
            RenderInfo<OM> originalModelInfo)

The output from the previous transformation is passed to the originalModelInfo parameter, while the sub-part to extract is specified by the Lambda Expression expression.

Last edited Jun 22, 2014 at 12:00 PM by frankabbruzzese, version 16


No comments yet.