I'm working with ASP.NET MVC 5 and Entity Framework 6, and I'm having a problem with the model selected value (only for decimal values) in my views.
That only happen when i'm getting data from database, if i try to submit the form, the value get back and is correctly selected.
This is my ViewModel
public class Regulacion
{
public IEnumerable<SelectListItem> ListaPorcentajePuntos
{
get
{
return new List<SelectListItem>
{
new SelectListItem { Text="0,625%" , Value = "0,625"},
new SelectListItem { Text="1%" , Value = "1"},
new SelectListItem { Text="1,25%", Value="1,25" },
new SelectListItem { Text="2,5%" , Value = "2,5"},
new SelectListItem { Text="5%" , Value = "5"},
};
}
}
public decimal? PorcentajePunto { get; set; }
//other stuff
public Regulacion(Reg reg)
{
PorcentajePunto = reg.PorcentajePunto;
}
}
And i'm using it in the view like this
@Html.DropDownListFor(x => x.PorcentajePunto, Model.ListaPorcentajePuntos, "Please select", new { @class = "form-control"})
I'm pretty sure, the problem become when i fetch the data from EF and decimal precision, because, when I'm debugging, the PorcentajePunto
property stored 5.00
instead of 5
, if i manually modify the property to 5
it works perfectly.
I read this question and try to use SelectList
instead of SelectListItem
but wasn't the solution
How can i deal with it?
Thanks!
EDIT
I'm fetching the Regulacion
data like this
using(var db = new DBTrafosContext())
{
var a = db.Reg.FirstOrDefault();
if(a != null){
Regulacion newRegulacion = new Regulacion(a);
}
}