0

I have a property called Campus2Id that being rendered as a dropdown using the built-in HtmlHelper in Razor. By default, unobstrusive and jquery validate is enabled.

When I do a form submit, somehow the ModelState shows that there is an error for that property; however, I did not add any validation attribute like Required to that property.

Is there a reason why Mvc marks this property having a validation error?

Model

[Display(Name = "Campus")]
public Int32 Campus2Id { get; set; }

View

<div class="form-group">
    @Html.LabelFor(model => model.Campus2Id, new { @class = "sr-only" })
    @Html.DropDownListFor(model => model.Campus2Id, new SelectList(Model.Campuses, "Id", "ShortName"), Model.CampusesLabel, new { @class = "form-control" })
    @Html.ValidationMessageFor(model => model.Campus2Id)
</div>

The error seems to be that the required validation is getting triggered on the Campus2Id field even though I did not specify [Required] attribute.

enter image description here

1
  • It would be helpful too see the error you are referring too. Commented Aug 13, 2014 at 19:16

1 Answer 1

1

I would suggest making the Campus2Id nullable.

That should resolve the validation error.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, making the field nullable (int?) fixed the problem.

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.