1

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

2 Answers 2

1
@Html.DropDownListFor(model => model.DataId,
                      new SelectList(ViewBag.Data as System.Collections.IEnumerable,
                      "DataId", "Name"), "Choose")
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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.