0

i have defined the following in my validation model class

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

Then i have created the DoctorID Selectlist as follow:-

     public ActionResult Create(int patientid)
        {
            Visit visit = new Visit();
            var allusers = Membership.GetAllUsers();

           ViewBag.DoctorID = new SelectList(allusers, "Username", "Username");
return View(visit);
        } 

and finally i define the dropdownlist at the view as follow:-

 <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>

but the problem i am facing is that incase the user leave the DoctorID dropdownlist empty then the [Required(ErrorMessage= "Please select a Doctor")] error will not be displayed? so what might be going wrong?

BR

Update:- here is the full view code:-

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

here is the Post action method code:-

   [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);
        }
John John
  • 1
  • 72
  • 238
  • 501
  • just a thought, but since `double` isn't nullable, it's never "blank" (default is 0.0) . Its possible this is screwing with the MVC validation engine. try making it a `double?` instead. – Thomas Jones Apr 25 '12 at 03:19
  • sorry the datatype should be string not double,, i updated my original code accordingly,, but still the Required data annotation is not working!!!! – John John Apr 25 '12 at 03:24
  • Where are you assigning the Visit_Validation class as the validator for Visit? – PinnyM Apr 25 '12 at 03:24
  • in my partial model class as follow:-. [MetadataType(typeof(Visit_Validation))] public partial class Visit { } – John John Apr 25 '12 at 03:26
  • In the view you are using, is the dropdown and validation code wrapped by a form tag? Validators don't appear unless a form is being used - see http://stackoverflow.com/questions/6465830/mvc-3-razor-html-validationmessagefor-not-working-in-partial-loaded-via-jquery – PinnyM Apr 25 '12 at 03:34
  • 1
    @johnG I dont believe `String.Empty` will trigger a Required Attribute either. `String.Empty != null` – Thomas Jones Apr 25 '12 at 03:36
  • @PinnyM Yes sure the dropdown and the validation code are wrapped by a form tag ,, i update my original question with the full code... – John John Apr 25 '12 at 11:36
  • @Kirean ,, but if i remove String.Empty then this means that a default selection will be viewed in the dropdownlist which i dot want to have ,, as there are 20 doctors and there is no reason to display one of them as the default selection in the dropdown list... – John John Apr 25 '12 at 11:38
  • @johnG have a look at this overload http://msdn.microsoft.com/en-us/library/dd492883.aspx I feel its the most often missed parameter in the Html.DropDownList method. Once you pass in your FieldName, and IEnum you specify an optionLabel. This is your "blank text" that is preselected when no option is selected. It gets submitted with a value of NULL. – Thomas Jones Apr 25 '12 at 13:50

1 Answers1

0

In order for validation to be triggered you need to have your POST controller action take the model as parameter:

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

or use the TryUpdateModel method:

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

When the form is submitted to this controller action the default model binder will invoke the validation rules contained in this Visit model. If your controller action never works with this model there's nothing out there that will ever interpret the data annotations that you put on it.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • thanks for the reply,,,, Yes my post action method takes the model as a parameter ,, the code which i showed is for the GEt action method.. i update my original code with the POST action method... – John John Apr 25 '12 at 11:41
  • In your POST controller action you seem to be redirecting if the model is valid. What do you do in the other case? Do you render the same view? – Darin Dimitrov Apr 25 '12 at 13:42
  • Yes i will render the same view ,, i update my original code with the full post action method code... – John John Apr 25 '12 at 23:39