Reputation: 1982
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!!
Upvotes: 1
Views: 2974
Reputation: 1982
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;
}
Upvotes: 0
Reputation: 1085
The data annotation will render data-val-required, because the DescFavorito
property is non-nullable.
Take a look at this link
Upvotes: 1