首页 > 解决方案 > DropDownListFor 不选择十进制值

问题描述

我正在使用 ASP.NET MVC 5 和 Entity Framework 6,并且在我的视图中遇到了模型选择值(仅适用于十进制值)的问题。

这只发生在我从数据库中获取数据时,如果我尝试提交表单,值会返回并被正确选择。

这是我的视图模型

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; 
        }

}

我在这样的视图中使用它

@Html.DropDownListFor(x => x.PorcentajePunto, Model.ListaPorcentajePuntos, "Please select", new { @class = "form-control"})

我很确定,当我从 EF 和小数精度获取数据时,问题就出现了,因为在我调试时,如果我手动修改属性,则PorcentajePunto存储的属性5.00而不是,它可以完美地工作。55

我读了这个问题并尝试使用SelectList而不是SelectListItem但不是解决方案

我该如何处理?

谢谢!

编辑

我正在获取这样的Regulacion数据

using(var db = new DBTrafosContext())
{
   var a = db.Reg.FirstOrDefault();
   if(a != null){
       Regulacion newRegulacion = new Regulacion(a);
   }
}

标签: c#asp.net-mvcentity-frameworkasp.net-mvc-5entity-framework-6

解决方案


推荐阅读