【问题标题】:DropDownList showing 4 decimal places when Database only has 1 decimal place C# MVC Postgres当数据库只有 1 个小数位时,DropDownList 显示 4 个小数位 C# MVC Postgres
【发布时间】:2017-06-09 10:58:17
【问题描述】:

在我的 Postgres 数据库中,我有一列用于 vatrate 的值如下

9、23、0 和 13.5

它们被设置为数字,只有一位小数,我的 ViewModel 对该 FK 列有以下注释

   //for vat rate  
    [DisplayFormat(DataFormatString = "{0:0.#}",  ApplyFormatInEditMode = true)]
    public Guid? vat_guid { get; set; }

在我看来,下拉列表有以下代码

   @Html.DropDownList("vat_guid", null, htmlAttributes: new { @class = "form-control" })

在我的控制器中

   ViewBag.vat_guid = new SelectList(db.q_vat, "vat_guid", "q_rate", pagedProduct.SingleOrDefault().vat_guid); 

但现在我对小数点值的查看结果是在该值上添加了 3 个额外的 0,我找不到导致此问题的原因

Dropdownlist on page

【问题讨论】:

  • [DisplayFormat] 仅适用于EditorFor()。你是如何在控制器中生成 SelectList 的。
  • 使用这一行 ViewBag.vat_guid = new SelectList(db.q_vat, "vat_guid", "q_rate", pagedProduct.SingleOrDefault().vat_guid); .我已更新问题以包含控制器代码。
  • 使用db.q_vat.ToList().Select(x => new SelectListItem{ Value = x.vat_guid, Text = x.q_rate.ToString("0:0.#") });构建您的SelectList
  • 如何更新视图中的行,按照您的方式构建列表后,我现在收到以下错误“没有具有键 'vat_guid 的 'IEnumerable' 类型的 ViewData 项'。”
  • 在生成<select> 的所有可能方式中,您选择了最糟糕的一种。对于该错误,请参阅this question/answer。你的编辑数据,所以第一步总是创建一个视图模型。

标签: c# asp.net-mvc postgresql html-select data-annotations


【解决方案1】:

尝试以下解决方案

[DisplayFormat(DataFormatString = "{0:0.0}",  ApplyFormatInEditMode = true)]
public Guid? vat_guid { get; set; }

【讨论】:

  • 你试过“{0:0.0}”
  • 阅读问题中的第一条评论!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-08
  • 1970-01-01
相关资源
最近更新 更多