I am binding a dropdown using Entitiyframework and i want to validate if the value is selected or not on a button click
public class DeviceModel
{
[Required(ErrorMessage = "Select a value")]
public SelectList DeviceListModel { get; set; }
[Required(ErrorMessage = "Select a value")]
public SelectList DeviceType { get; set; }
}
public ActionResult Index()
{
List<Example> mod = (from data in obj.Examples select data).ToList();
Example exp = new Example();
exp.Name = "--Select--";
exp.ID = 0;
mod.Insert(0, exp);
SelectList modeldata = new SelectList(mod, "ID", "Name", 0);
DeviceModel objmodel = new DeviceModel();
objmodel.DeviceListModel = modeldata;
List<Gearhead_Type> types = (from data in obj.Gearhead_Type select data).ToList();
Gearhead_Type GTypre = new Gearhead_Type();
GTypre.Name = "--Select--";
GTypre.ID = 0;
types.Insert(0, GTypre);
SelectList asd = new SelectList(types, "ID", "Name", 0);
objmodel.DeviceType = asd;
return View(objmodel);
}
<div class="form-group">
@Html.Label("Gearhead Model and Type ", new { @class = "col-md-6 control-label ", @style = "padding-left:200px" })
@Html.DropDownListFor(s=>s.DeviceListModel, Model.DeviceListModel)
@Html.ValidationMessageFor(s => s.DeviceListModel)
</div>
But the validation is not working in the button click.