Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Maybe you want to make something yourself?</p> <pre><code> public delegate object Property&lt;T&gt;(T property); public static HtmlString MultiSelectListFor&lt;TModel, TKey, TProperty&gt;( this HtmlHelper&lt;TModel&gt; htmlHelper, Expression&lt;Func&lt;TModel, IEnumerable&lt;TKey&gt;&gt;&gt; forExpression, IEnumerable&lt;TProperty&gt; enumeratedItems, Func&lt;TProperty, TKey&gt; idExpression, Property&lt;TProperty&gt; displayExpression, Property&lt;TProperty&gt; titleExpression, object htmlAttributes) where TModel : class { //initialize values var metaData = ModelMetadata.FromLambdaExpression(forExpression, htmlHelper.ViewData); var propertyName = metaData.PropertyName; var propertyValue = htmlHelper.ViewData.Eval(propertyName).ToStringOrEmpty(); var enumeratedType = typeof(TProperty); //check for problems if (enumeratedItems == null) throw new ArgumentNullException("The list of items cannot be null"); //build the select tag var returnText = string.Format("&lt;select multiple=\"multiple\" id=\"{0}\" name=\"{0}\"", HttpUtility.HtmlEncode(propertyName)); if (htmlAttributes != null) { foreach (var kvp in htmlAttributes.GetType().GetProperties() .ToDictionary(p =&gt; p.Name, p =&gt; p.GetValue(htmlAttributes, null))) { returnText += string.Format(" {0}=\"{1}\"", HttpUtility.HtmlEncode(kvp.Key), HttpUtility.HtmlEncode(kvp.Value.ToStringOrEmpty())); } } returnText += "&gt;\n"; //build the options tags foreach (TProperty listItem in enumeratedItems) { var idValue = idExpression(listItem).ToStringOrEmpty(); var displayValue = displayExpression(listItem).ToStringOrEmpty(); var titleValue = titleExpression(listItem).ToStringOrEmpty(); returnText += string.Format("&lt;option value=\"{0}\" title=\"{1}\"", HttpUtility.HtmlEncode(idValue), HttpUtility.HtmlEncode(titleValue)); if (propertyValue.Contains(idValue)) { returnText += " selected=\"selected\""; } returnText += string.Format("&gt;{0}&lt;/option&gt;\n", HttpUtility.HtmlEncode(displayValue)); } //close the select tag returnText += "&lt;/select&gt;"; return new HtmlString(returnText); } </code></pre>
 

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