Let’s say I have the following selectlist (Countries) in a ViewModel:
//..
private static string[] _countries = new[] {
"USA",
"Canada",
"Japan"
};
//...
SelectList Countries = new SelectList(_countries, dinner.Country);
//...
And I render a dropdown list in the following fashion:
<%: Html.DropDownListFor(m => m.Dinner.Country, Model.Countries) %>
I noticed that using firebug, I can inject my own values into the DropDownList and that value may be inserted into the database.
What is the best way to validate that there are no injected values (preferably a DRY method)?
I would recommend taking advantage of DataAnnotations and create your own custom validation attribute.
This provides a way to encapsulate your validation logic (satisfying your DRY requirement), and will be applied server-side (preventing html manipulations like the one you described).