所需的数据注释不适用于我的asp.net mvc3中的DropDownlist

时间:2012-04-25 03:18:02

标签: asp.net-mvc-3 data-annotations

我在验证模型类中定义了以下内容

public class Visit_Validation
{
   [Display(Name = "Assign to Doctor")]
   [Required(ErrorMessage= "Please select a Doctor")]
   public string DoctorID { get; set; }}

然后我创建了DoctorID Selectlist,如下所示: -

     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);
        }

1 个答案:

答案 0 :(得分:0)

为了触发验证,您需要让POST控制器操作将模型作为参数:

[HttpPost]
public ActionResult Create(Visit visit)
{
    ...
}

或使用TryUpdateModel方法:

[HttpPost]
public ActionResult Create()
{
    Visit visit = new Visit();
    if (!TryUpdateModel(visit))
    {
        // validation failed
    }
    ...
}

当表单提交给此控制器操作时,默认模型绑定器将调用此访问模型中包含的验证规则。如果您的控制器操作永远不适用于此模型,那么就没有任何内容可以解释您放在其上的数据注释。