I have a View page which contains a dropdownlist of values populated from a custom constructor class.
The DDL is populated with a SelectList created from a list of custom items as follows:
public class CustomerType
{
public int Id { get; set; }
public string Name { get; set; }
public string ValidationRule { get; set; }
}
In my View I then set this as so: @Html.DropDownListFor(model => model.Id, (IEnumerable) Model.SelectListOfCustomItems, "Please select...")
What I want to try and do is display the ValidationRule property for each item selected, and show this in a Label.
I figure I have to use JavaScript to do this, but I'm unsure how to actually get that ValidationRule property? I could probably get the selected item using code similar to the below, but I can't see how to drill down to get other data?
var dropDown = document.getElementById("MyDropDownListName");
dropDown.onchange = function () { DisplayValidationRule(this); };
function DisplayValidationRule(ddl)
{
document.getElementById('lblRule').textContent = [Unknown]
}
The bit I'm missing is where I've marked it [Unknown], as I don't have a club how to get this. I was thinking maybe something like ddl.items[ddl.selectedItem].value['ValidationRule'] but that doesn't work for me.