Validate dropdown item does not occur more than once in a list of dropdown fields with the same id

后端 未结 1 991
清歌不尽
清歌不尽 2021-01-21 07:41

I have a form where the user can add as many rows as needed. Each time they are expected to select a different option from the dropdown list provided. At the moment there is no

1条回答
  •  终归单人心
    2021-01-21 08:10

    This is simply not possible using a ValidationAttribute (either in-built or custom ones) and MVC's unobtrusive client side validation.

    Validation attributes are applied to properties of a model (your class), and the context in which validation is checked is for that instance only - it has no knowledge of other instances of the model within the collection, therefore it cannot compare other instances.

    Conversely if you apply a ValidationAttribute to a collection (e.g. List property), unobtrusive validation will not work because the data-val-* attributes necessary to add rules to the $.validator could only be generated if you include an input for the collection property (as opposed to each property of each object in the collection) which means ModelBinding would fail.

    You will need to write your own controller code and scripts to achieve your custom validation.

    On the client side, you could handle the .change() event of the .... to prevent the user making invalid selections in the first place, but that becomes more complex if your dynamically adding/deleting items, and/or when your view is editing existing data where the property already has a selected value (I'll leave that to you to ask a new question showing your attempt if you want to implement that).

    On the server side, you can check for duplicate values, and if so, add a ModelState error and return the view, for example

    var selected = new List();
    for (int i = 0 i < model.Count; i++)
    {
        if (selected.Contains(model[i].YourProperty))
        {
            ModelState.AddModelError("", "Please make a unique selection");
            break;
        }
        else
        {
            selected.Add(model[i].YourProperty);
        }
    }
    if (!ModelState.IsValid)
    {
        return View(model);
    }
    ....
    

    or using linq

    if (model.Select(x => x.YourProperty).GroupBy(x => x).Any(g => g.Count() > 1))
    {
        ModelState.AddModelError("", "Please make a unique selection");
    }
    

    which would then be displayed in the views @Html.ValidationSummary().

    If your using @Html.ValidationMessageFor(m => m[i].YourProperty) in your view for each dropdownlist, then the above loop can be modified to

    if (selected.Contains(model[i].YourProperty))
    {
        var propertyName = string.Format("[{0}].yourPropertyName", i);
        ModelState.AddModelError(propertyName, "Please make a unique selection");
        break;
    }
    

    and modify the script to add/remove the message for the element generated by ValidationMessageFor() (i.e instead of the

    element as shown above)

    As a side note, if you want to learn more about how validation attributes in conjunction with client side validation work, I recommend reading The Complete Guide To Validation In ASP.NET MVC 3 - Part 2.

    0 讨论(0)
提交回复
热议问题