In your controller When the model is not valid , repopulate the dropdown:
if (ModelState.IsValid)
{
IEnumerable<SelectListItem> countries = _DB.Countries.Where(x => x.Status == Status.Visible)
.Select(x => new SelectListItem()
{
Value = x.ID + "",
Text = "(+" +x.PhoneCountryCode + ") - " + x.Name
}).ToList();
countries.First().Selected = true;
}
else
{
//We need to rebuild the dropdown or we're in trouble
IEnumerable<SelectListItem> countries = _DB.Countries.Where(x => x.Status == Status.Visible)
.Select(x => new SelectListItem()
{
Value = x.ID + "",
Text = "(+" +x.PhoneCountryCode + ") - " + x.Name
}).ToList();
countries.First().Selected = true;
}
You can also check the errors in the model state by using this. Might be something interesting there:
var errors = ModelState
.Where(x => x.Value.Errors.Count > 0)
.Select(x => new { x.Key, x.Value.Errors })
.ToArray();