How to validate a Ienumerable property using Dataannotations?

Asked

Viewed 121 times

0

I have a IEnumerable<long> selectedItems he is to display a select to select multiple values, I want to validate this IEnumerable using the DataAnnotations to check if it is empty because it needs at least to have a selected value. I am trying to use the Required but it’s not working. How can I do it ?

I’m trying like this.

Model

public class EmpresaModel{
    [Required(ErrorMessage = "Selecione ao menos uma forma de pagamento disponível")]
    public IEnumerable<long> selectedItems { get; set; }
}

HTML

<div class="form-group">
    <label for="@Html.IdFor(model => model.selectedItems)" class="cols-sm-2 control-label">Formas de pagamento disponíveis <img src="~/Imagens/required.png" height="6" width="6"></label>
    @Html.ListBoxFor(model => model.selectedItems, Model.formasPagto, new { Class = "form-control", placeholder = "Selecione as formas de pagamento disponíveis", @multiple = true})
    @Html.ValidationMessageFor(model => model.formasPagto)
</div>

1 answer

0


There is an error in HTML in ValidationMessageFor it should be like this:

@Html.ValidationMessageFor(model => model.selectedItems)

and the value that was formasPagto was wrong.

Full html:

@using (Html.BeginForm("Store", "Home", FormMethod.Post))
{
    <div class="form-group">
        <label for="@Html.IdFor(model => model.selectedItems)" class="cols-sm-2 control-label">Formas de pagamento disponíveis<img src= "~/Imagens/required.png" height= "6" width= "6" ></label>
        @Html.ListBoxFor(model => model.selectedItems, new List<SelectListItem> { new SelectListItem() { Value = "1", Text = "1" }, new SelectListItem() { Value = "2", Text = "2" } }, new { Class = "form-control", placeholder = "Selecione as formas de pagamento disponíveis" })
        @Html.ValidationMessageFor(model => model.selectedItems)
    </div>
    <button>Enviar</button>
}

the Validation you have placed is correct is Required even what was missing I believe to be the JQuery and its complements for validation:

<script src="/Scripts/jquery-1.10.2.js"></script>

<script src="/Scripts/jquery.validate.js"></script>

<script src="/Scripts/jquery.validate.unobtrusive.js"></script>

Have a file that is better to configure it in the folder App_Start in the archive BundleConfig, make a change like this:

namespace WebApp
{
    public class BundleConfig
    {
        // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js", 
                        "~/Scripts/jquery.validate*"));

that is, adding "~/Scripts/jquery.validate*" to make the first client-side validation.

  • needed to remove the @multiple = true tbm in HTML. But now it worked blah. thank you.

  • @Fernandopaiva I used with Multiple and was also right, but, all right that matters that your doubt has been solved! and did the editing to stay the way it was the solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.