c# - 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
而不是,它可以完美地工作。5
5
我读了这个问题并尝试使用SelectList
而不是SelectListItem
但不是解决方案
我该如何处理?
谢谢!
编辑
我正在获取这样的Regulacion
数据
using(var db = new DBTrafosContext())
{
var a = db.Reg.FirstOrDefault();
if(a != null){
Regulacion newRegulacion = new Regulacion(a);
}
}
解决方案
推荐阅读
- javascript - 从打字稿中的数据填充列中的索引值(Angular / React)
- wordpress - Wordpress Multisite - 成熟的选项 - 它有什么作用?
- hive - 如何在 HiveQL 中将列和行作为标题
- botkit - botkit google hangouts chat 是 0.7 版还是 google hangouts chat 支持打字指示器?
- ios - 在 iOS 应用程序中设置 Microsoft Azure SAML 2.0
- three.js - 覆盖网格对于 Forge 3D 查看器中的某些材质颜色是透明的
- tensorflow - 在 Beagle Bone Black 中安装张量流
- mongodb - 在多个相同的集合中查找不同的字段 mongodb
- linux - 使用 ubuntu:16.04 运行 docker 容器实际上运行的是 ubuntu 18.04
- python - 为什么在另一个函数中重新定义一个函数不起作用?(蟒蛇龟)