1

I'm having trouble in displaying validation message when a null value is selected.

My code for the dropdownlist:

<strong>@Html.ValidationSummary("", new { @class = "text-danger" })</strong>
@Html.DropDownList("Category", null, String.Empty, new { @class = "form-control" })

I understand that String.Empty equals to a null value(Correct me if i'm wrong).

My code for my model:

namespace Mp.Models
{
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;

  public partial class Content
  {
      [Required(ErrorMessage: "A Category is required!")]
      public string Category { get; set; }
  } 
}
11
  • possible duplicate of validate a dropdownlist in asp.net mvc Commented Aug 3, 2015 at 8:40
  • Have you included the relevant ValidationMessage() helper? Does the html your generating include <option value=""></option>? Commented Aug 3, 2015 at 8:42
  • @StephenMuecke I'm populating my dropdownlist from database. I'm adding Empty.String so the first value displayed on the dropdownlist would be blank. Commented Aug 3, 2015 at 9:29
  • Yes I know that. Were you intending to answer my queries? Commented Aug 3, 2015 at 9:31
  • 1
    I assume that's @Html.ValidationSummary and not @ValidationSummary? Commented Aug 3, 2015 at 9:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.