Skip to content

Instantly share code, notes, and snippets.

@fluxdigital
Created November 28, 2018 14:35
Show Gist options
  • Save fluxdigital/65a5b150f7214a3cf8d8efa5fe52823e to your computer and use it in GitHub Desktop.
Save fluxdigital/65a5b150f7214a3cf8d8efa5fe52823e to your computer and use it in GitHub Desktop.
WFFM custom drop down field (used in MVC)
public class CustomDropDownListField : DropListField
{
public CustomDropDownListField()
{
//initalise services here
}
private string InitialSelection { get; set; }
public override void Initialize()
{
var customItems = null; //get your list of items here from your service
selectItems = new List<SelectListItem>();
if (Parameters.ContainsKey("initialselection"))
InitialSelection = Parameters["initialselection"];
var CustomSelected = false;
foreach (var item in customItems)
{
if (string.Equals(item.Name, InitialSelection, StringComparison.InvariantCultureIgnoreCase))
CustomSelected = true;
selectItems.Add(new SelectListItem
{
Value = item.Name,
Text = item.Name,
Selected = CustomSelected
});
}
if (Parameters.ContainsKey("showemptychoice"))
{
var emptyChoiceText = Parameters.ContainsKey("emptychoicetext") &&
!string.IsNullOrEmpty(Parameters["emptychoicetext"])
? Parameters["emptychoicetext"]
: "please select";
var showEmptyChoice = Parameters["showemptychoice"] == "Yes";
if (showEmptyChoice)
Items.Insert(0,
new SelectListItem {Text = string.Format("-- {0} --", emptyChoiceText), Value = string.Empty});
}
EmptyChoise = false;
base.Initialize();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment