我在验证模型类中定义了以下内容
public class Visit_Validation
{
[Display(Name = "Assign to Doctor")]
[Required(ErrorMessage= "Please select a Doctor")]
public string DoctorID { get; set; }}
然后我创建了 DoctorIDSelectlist
如下: -
public ActionResult Create(int patientid)
{
Visit visit = new Visit();
var allusers = Membership.GetAllUsers();
ViewBag.DoctorID = new SelectList(allusers, "Username", "Username");
return View(visit);
}
最后我dropdownlist
在视图中定义如下: -
<div class="editor-label">
@Html.LabelFor(model => model.DoctorID)
</div>
<div class="editor-field">
@Html.DropDownList("DoctorID", String.Empty)
@Html.ValidationMessageFor(model => model.DoctorID)
</div>
但我面临的问题是,如果用户将 DoctorID 下拉列表留空,则[Required(ErrorMessage= "Please select a Doctor")]
不会显示错误?那么可能出了什么问题?
BR
更新:- 这是完整的视图代码:-
<h2>Create</h2>
@section scripts{
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>}
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Visit</legend>
<div class="editor-label">
@Html.LabelFor(model => model.VisitTypeID, "VisitType")
</div>
<div class="editor-field">
@Html.DropDownList("VisitTypeID", String.Empty)
@Html.ValidationMessageFor(model => model.VisitTypeID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Date)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Date, new { value = "FL", disabled = "disabled" })
@Html.ValidationMessageFor(model => model.Date)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Note)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Note)
@Html.ValidationMessageFor(model => model.Note)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DoctorID)
</div>
<div class="editor-field">
@Html.DropDownList("DoctorID", String.Empty)
@Html.ValidationMessageFor(model => model.DoctorID)
</div>
<div class="editor-label">
Visit Status
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.VisitStatu.Description, new { value = "FL", disabled = "disabled" })
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CreatedBy)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.CreatedBy, new { value = "FL", disabled = "disabled" })
@Html.ValidationMessageFor(model => model.CreatedBy)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
这是 Post 操作方法代码:-
[HttpPost]
public ActionResult Create(Visit visit)
{
if (ModelState.IsValid)
{
visit.StatusID = repository.GetVisitStatusByDescription("Assinged");
visit.CreatedBy = User.Identity.Name;
visit.Date = DateTime.Now;
repository.AddVisit(visit);
repository.Save();
return RedirectToAction("Index");
}
ViewBag.DoctorID = new SelectList(Membership.GetAllUsers(), "Username", "Username");
ViewBag.StatusID = new SelectList(repository.FindAllVisitStatus(), "StatusID", "Description");
ViewBag.VisitTypeID = new SelectList(repository.FindAllVisitType(), "VisitTypeID", "Description");
return View(visit);
}