Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.