1

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);
   }
}
Juan Salvador Portugal
  • 1,233
  • 4
  • 20
  • 38
  • Its not clear what you are asking. Both `5` and `5.00` have the same representation. Are you wanting to specifically save the selected value as `5` instead of `5.00`, and if so why? –  Sep 03 '18 at 11:41
  • Saving `5.00` is correct, my problem, is when i'm retriving the `decimal` it must be `5`, other case would be `2.5`, it must save like `2.50` because decimal precision, but when i'm retriving i want to get `2.5` because `2.50` cant match to the `List` it is understood? – Juan Salvador Portugal Sep 03 '18 at 11:44
  • @StephenMuecke basically, if i try to set `PorcentajePunto = new decimal(5.00)`, `PorcentajePunto` value is `5` and it works, but if i retrive with Entity Framework, `PorcentajePunto` value is `5.00` instead of `5`, so it is not working, do you understand me? – Juan Salvador Portugal Sep 03 '18 at 11:50
  • But they are both the same value (as it `5.0000000)` - I am not sure what you are wanting to achieve - and when you save it to the db, its going to be `5.00` anyway –  Sep 03 '18 at 12:02
  • @StephenMuecke exactly, the problem, is that DropDownListFor() is not selecting the option when the value is `5.00` and it is selecting it ok if is `5` thats why i want to remove the extra 0 to the `decimal` and keep it as `decimal` – Juan Salvador Portugal Sep 03 '18 at 12:04
  • But why not just make it `Value = "5.00"`? –  Sep 03 '18 at 12:10
  • I'm actually fighting with this problem since friday and I did not think to solve it that way, thanks so much – Juan Salvador Portugal Sep 03 '18 at 12:12
  • @StephenMuecke Thanks! when you can, answer the question to mark it as correct! – Juan Salvador Portugal Sep 03 '18 at 12:13
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179320/discussion-between-stephen-muecke-and-juan-salvador-portugal). –  Sep 03 '18 at 12:17
  • After a whole day of headbanging with the same problem we solved it with an extra string property to "transport" between the original decimal and the dropdown... – marto Feb 19 '20 at 13:36

0 Answers0