0

So I have a drop down list that for some reason is getting the input-validation-error class injected. When I first load the page everything loads fine on the GET request. It isn't until the form is POSTed back will it invalidate. No matter which option is selected, on the post MVC will inject the error class, and the error message will say "The value '0' is invalid". The value it displays is the value of the selection which could be any valid integer.

Here is the CSHTML:

<div class="form-group">
            @Html.LabelFor(m => m.DistributionTable, "Distribution:", new { @class = "col-lg-2 control-label" })
            <div class="col-lg-10">
                @Html.DropDownListFor(m => m.DistributionTable, new SelectList(Model.DistributionTable, "Value", "Key", Model.Criteria.SelectedDistribution), new { @class = "form-control" })
                @Html.ValidationMessageFor(m => m.DistributionTable)
            </div>
        </div>

So from there you can see the drop down list is binded to a field DistributionTable that looks like this:

    [XmlIgnore]
    public Dictionary<string, int> DistributionTable { get; set; }

As you can see this is a Dictionary that has no attributed validation. Nor, have I added any custom validation past once it arrives in the controller.

My question is why and where is the validation error occurring, so that I can remove this validation error?

Thank you,

jazzmasterkc
  • 379
  • 6
  • 17
  • 1
    olease check [this answer](https://stackoverflow.com/a/14745437/6448640) –  Jan 10 '18 at 07:20
  • 1
    You cannot bind a ` binds to, and posts back a single value (the value of the selected option). Your model needs a property to bind to (say `int SelectedValue`). I suggest you study the code in both [this answer](https://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) and [this answer](https://stackoverflow.com/questions/41719293/mvc5-how-to-set-selectedvalue-in-dropdownlistfor-html-helper/41731685#41731685) –  Jan 10 '18 at 08:01

1 Answers1

0

Thank you ashik and Stephen Muecke. The comments lead me to refactor to this code which ended up working. The validation error doesn't get injected now the dropdownlistfor is binded to the selected item as opposed to the actual Dictionary of options.

        <div class="form-group">
            @*@Html.LabelFor(m => m.DistributionTable, "Distribution:", new { @class = "col-lg-2 control-label" })*@
            @Html.LabelFor(m => m.Criteria.SelectedDistribution, "Distribution:", new { @class = "col-lg-2 control-label" })
            <div class="col-lg-10">
                @*@Html.DropDownListFor(m => m.DistributionTable, new SelectList(Model.DistributionTable, "Value", "Key", Model.Criteria.SelectedDistribution), new { @class = "form-control" })*@
                @Html.DropDownListFor(m => m.Criteria.SelectedDistribution, new SelectList(Model.DistributionTable, "Value", "Key"), new { @class = "form-control" })
                @*@Html.ValidationMessageFor(m => m.DistributionTable)*@
                @Html.ValidationMessageFor(m => m.Criteria.SelectedDistribution)
            </div>
        </div>
jazzmasterkc
  • 379
  • 6
  • 17