Question

In an ASP.NET MVC 4 application I have a DropDownListFor to show a favourites list. This field is no required to submit the form. The DropDownListFor get the data from a master data table. The thing is that when I submit and I haven't choose a favourite from the list, I am not able to disable the valildation. This is the kendo DropDownListFor code:

    @{
IList<Repsol.Portal.PortalClienteDEAC.GLP.Domain.Entities.PedidoFavorito> cmbFavorito = listaPedidoFavorito.ToList(); 
}

@(Html.Kendo().DropDownListFor(model => model.pedidoFavorito.IdFavorito)
                    .BindTo(new SelectList(cmbFavorito.Select(s => new { Key = s.IdFavorito, Value = s.DescFavorito }), "Key", "Value"))
                    .Name("IdFavorito")
                    .OptionLabel(Idioma.Shared.Pedidos_SeleccioneOpcion)
                    )

This is the content of the class PedidoFavorito.cs:

public partial class PedidoFavorito
{
    #region Primitive Properties

    public virtual int IdFavorito
    {
        get;
        set;
    }

    public virtual string IdContrato
    {
        get;
        set;
    }

    public virtual string IdPedido
    {
        get;
        set;
    }

    public virtual string DescFavorito
    {
        get;
        set;
    }

    public virtual string metadata
    {
        get;
        set;
    }

    #endregion

}

I've tried so many things but without a positive result.

Thanks in advance!!

Was it helpful?

Solution 2

I've found a solution. I have to do 2 things. In the view, I have to add this script:

$(document).ready(function () {
    document.getElementById("IdFavorito").removeAttribute("required");;
});

An also I have to make nullable the IdFavorito in PedidoFavorito.cs:

public virtual int? IdFavorito
    {
        get;
        set;
    }

OTHER TIPS

The data annotation will render data-val-required, because the DescFavorito property is non-nullable.

Take a look at this link

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