Question

Here is my model

public class SchoolUser
{
    public int schoolId { get; set; }

    public int contact1UserId { get; set; }

    public IEnumerable<DataLayer.salutations> Salutations { get; set; }

    public int departmentId { get; set; }

    public IEnumerable<DataLayer.departments> Departments { get; set; }

    [Required]
    public int roleId { get; set; }

    public IEnumerable<DataLayer.rolesView> roles { get; set; }

Here is my razor code for creating dropdown for deptartmentid view

                <div style="width:75%; display:inline-block; margin-left:5px;">
                    @Html.DropDownListFor(model => model.departmentId, new SelectList(Model.Departments, "deptId", "description"), "")
                    @Html.ValidationMessageFor(model => model.departmentId)
                </div>                    

What confuses us to no end, razor validation complains when no dept is chosen. But the school model had no [required] attribute.

"The departmentId field is required."

Looking at page source I see

 <select data-val="true" data-val-number="The field departmentId must be a number." data-val-required="The departmentId field is required." id="departmentId" name="departmentId"><option value=""></option>
<option value="30">English</option>
</select>

For some reason, the razor view engine has 'decided' to require this field to be populated even though the model has no [required] or other constraining attribute.

Was it helpful?

Solution

It is not nullable. You should change it to int? .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top