ASP.NET MVC. Validation is not performed on the drop-down menu regardless of the value

I have a dropdown form in my MVC application. Now, when I try to add validation to the mix, it seems that the dropdown does not perform validation, regardless of what the value is.

Without verification, this will allow the controller to work and redirect as planned. On validation, it seems to allow database changes, but ModelState.IsValid is false.

I am stuck. Is this a known issue?

View:

<label for="parent">Child of:</label>
<%= Html.DropDownList("parent", (SelectList)ViewData["pageList"])%>
<%= Html.ValidationMessage("parent") %>

Controller action:

[AcceptVerbs(HttpVerbs.Post)]
[ValidateInput(false)]
[ValidateAntiForgeryToken()]
public ActionResult Create(Page page)
{
    try
    {
        pageRepository.Insert(page);
    }
    catch (RuleException ex)
 {  
     ex.CopyToModelState(ModelState);
 }

 if (!ModelState.IsValid)
 {
     var pageSelectList = pageRepository.GetTop().ToList();
     pageSelectList.Add(new Page
 {
     menuTitle = "None"
 });
     ViewData["pageList"] = new SelectList(pageSelectList.OrderBy(x => x.listOrder), "ID", "menuTitle");
     return View();
 }
 return RedirectToAction("List");
}

Error returned: The value "x" is incorrect. Where "x" is the numerical value of the current selection. The failure occurs regardless of the selected value.

public class Page
{
    private EntityRef<Page> _parent = default(EntityRef<Page>);
    private EntitySet<Page> _children = new EntitySet<Page>();

    public int ID { get; set; }
    public string pageTitle { get; set; }
    public string menuTitle { get; set; }
    public string content { get; set; }
    public int listOrder { get; set; }
    public bool visible { get; set; }
    public int parent { get; set; }
    public DateTime? created { get; set; }
    public DateTime? edited { get; set; }
    public string createdBy { get; set; }
    public string lastEditBy { get; set; }
    public string linkInfo { get; set; }
    public bool IsSelected { get; set; }

    public Page Parent
    {
        // return the current entity
        get { return this._parent.Entity; }
        set { this._parent.Entity = value; }
    }

    public EntitySet<Page> Children
    {
        get { return this._children; }
        set { this._children.Assign(value); }
    }

    public static Page Error404()
    {
        return (new Page
        {
            content = "<p>Page not found</p>",
            pageTitle = "404.  Page not found"
        });
    }   
}

Here is what I tried for a workaround:

public ActionResult Create([Bind(Exclude="parent")] Page page)
{
    page.parent = Convert.ToInt32(Request.Form["parent"]);
...

ModelBinding Request.Form. ?

0
2

ModelState [ "parent" ]. Value.AttemptedValue entity, int.

0

RuleException? , - , , "parent" . , , . , , , , .

0

All Articles