Reputation: 163
I have the following set for drop down in asp.net mvc
@Html.DropDownListFor(model => model.DataId, ((IEnumerable<ProgrammeModel>)ViewBag.Data).Select(option => new SelectListItem
{
Text = (option == null ? "None" : option.Name),
Value = option.DataId.ToString(),
Selected = (Model != null) && (option.DataId== Model.DataId)
}), "Choose...", new { Class = "input", id = "DataId"
})
And in model:
[Required(ErrorMessage="The Data field is required.")]
public int DataId { get; set; }
But when the validation happens on form submit I am getting the error message for this field as
The Int32 field is required.
where I was expecting the result as
The Data field is required
Upvotes: 1
Views: 409
Reputation: 16
I had the same problem, It seems that in the controller you have ViewBag.DataId and it is causing confusion to the validation. Please try removing it and let me know if it is ok now.
Thanks, Dimitar
Upvotes: 0
Reputation: 179
@Html.DropDownListFor(model => model.DataId,
new SelectList(ViewBag.Data as System.Collections.IEnumerable,
"DataId", "Name"), "Choose")
Upvotes: 1