【发布时间】:2019-10-07 08:30:25
【问题描述】:
我想为下拉列表实现客户端验证。
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>
名称字段是必需的,如果表单提交时它是空白的,客户端验证工作正常。但是,下拉列表仅适用于服务器端验证。
【问题讨论】:
标签: c# asp.net linq model-view-controller dropdownlistfor