1

I have this code in my view:

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

...  

    <div class="modal_label">
        @Html.LabelFor(model => model.Organization.Type)
    </div>
    <div class="modal_field">
        @Html.DropDownListFor(model => model.Organization.Type, (IEnumerable<SelectListItem>)ViewBag.TypeList, String.Empty)
        @Html.ValidationMessageFor(model => model.Organization.Type)
    </div>

If I change @Html.DropDownFor to @Html.EditorFor, then validation is working, but in this case I have following html rendered:

 <select id="Organization_Type" name="Organization.Type" class="valid">
 ...
 </select>

Here is my model:

[MetadataType(typeof(OrganizationMetaData))]
public partial class Organization
{

}

public class OrganizationMetaData
{
    [Required(ErrorMessageResourceType = typeof(CCMESResources.ResourceErrors),ErrorMessageResourceName = "ValueIsRequired")]
    public int Type { get; set; }
}

When the form posts, there ARE errors in ModelState. Can you help me?

karaxuna
  • 26,752
  • 13
  • 82
  • 117
  • You could add @Html.ValidationSummary() at top of your view and show us exactly what error you get. Are you sure it is related to Type property? – archil May 01 '12 at 14:58
  • It doesn't show any validation message until it posts and there is an error in controller – karaxuna May 01 '12 at 15:01
  • Possible duplicate of [Validating required selection in DropDownList](http://stackoverflow.com/questions/4672289/validating-required-selection-in-dropdownlist) – KyleMit Sep 28 '16 at 05:16

2 Answers2

3

Make sure that you have used a nullable type on your view model:

[Required]
public int? Type { get; set; }

In your model you seem to have used a non-nullable integer which is not coherent with what you are trying to achieve on your view.

Also make sure that the controller action that you are POSTing this form to takes the view model as action parameter.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I have changed int to int? but it's not working... Also I have done this kind of validation in other view, where parameter is NOT nullable and it works fine – karaxuna May 01 '12 at 14:44
  • Can you provide a full and yet concise example allowing us to reproduce the problem? – Darin Dimitrov May 01 '12 at 14:46
  • The truth is that the fact that Type is not nullable, doesn't change anything. It IS validating on the server side – karaxuna May 01 '12 at 14:55
1

In your model (or view model), when you assign a value to it from a drop down list, if the first selected value is an empty string or null, it "should" trigger validatation, but it will take a trip to the server to do it. I've been unsuccessful in getting unobtrusive validation to work client side without doing a post first. Using a nullable field for a required value generally is not a good idea. Also, because you're not using a nullable field, it should force validation when you check to see if the model is valid. Here's a snippet from my project which works (also, I'm using the data annotation extensions for the "Min" annotation):

Model:

[Display(Name = "Ticket Priority")]
[Min(1, ErrorMessage = "You must select a ticket priority.")]
public int TicketPriorityID { get; set; }

View:

<div class="editor-label">
    @Html.LabelFor(model => model.TicketPriorityID)
</div>
<div class="editor-field">
    @Html.DropDownList("TicketPriorityID", string.Empty)
    @Html.ValidationMessageFor(model => model.TicketPriorityID)
</div>

Controller (HttpGet):

ViewBag.TicketPriorityID = new SelectList(db.TicketPriorities.OrderBy(x => x.TicketPriorityID).ToList(), "TicketPriorityID", "Name");

Controller (HttpPost):

if (ModelState.IsValid)
{
    ...
}
Eric Garrison
  • 291
  • 1
  • 10
  • 21
  • Validation works correctly with Required attribute. I see this in ModelState where there is an error that Organization Type is required. I don't understand why it doesn't work on client side – karaxuna May 01 '12 at 14:49
  • I think it has something to do with the dropdownlist. I've read before that you shouldn't even validate a drop down list, but in this case, if you want an empty value to be the default, you need to. I can't give you a good answer on why this doesn't work client side though. I have the same issue. – Eric Garrison May 01 '12 at 14:55
  • Ok, thanks very much Eric Garrison. Now i should go home. If you find answer on your question, let me know please. – karaxuna May 01 '12 at 14:57