I would like to implement client side validation for a drop down list.
Model
public partial class Beach
{
public int Beach_ID{ get; set; }
[Required]
public string NAME { get; set; }
[Required]
public Nullable<int> LOCATION_ID { get; set; }
public virtual LOCATION LOCATION { get; set; }
}
Controller
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
BEACH BEACH = db.BEACH .Find(id);
if (BEACH == null)
{
return HttpNotFound();
}
ViewBag.LOCATION = new SelectList(db.LOCATION, "LOCATION_ID", "LOCATION_NAME", BEACH.LOCATION_ID);
return View(BEACH);
}
View
<div class="form-group">
<div class="col-md-10">
@Html.DropDownList("LOCATION", null, "Select Location", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.LOCATION_ID, "", new { @class = "text-danger" })
</div>
</div>
The name field is required and if the form is submitted with it being blank, the client side validation works fine. However, the dropdown list only works with server side validation.