Created
November 28, 2018 14:35
-
-
Save fluxdigital/65a5b150f7214a3cf8d8efa5fe52823e to your computer and use it in GitHub Desktop.
WFFM custom drop down field (used in MVC)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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