1

In my Postgres DB, I have a column for vatrate with the following values

9, 23, 0 and 13.5

They are set as numeric, with only 1 decimal place, and my ViewModel has the following annotation for that FK column

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

In my view I have the following code for the dropdownlist

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

In my controller I have

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

But now my view result for the value with a decimal place is adding 3 extra 0s to that value, and I cannot find whats causing this issue

Dropdownlist on page

LavsTo
  • 129
  • 4
  • 19
  • 1
    `[DisplayFormat]` is only applicable to `EditorFor()`. How are you generating the SelectList in the controller. –  Jun 09 '17 at 11:00
  • with this line ViewBag.vat_guid = new SelectList(db.q_vat, "vat_guid", "q_rate", pagedProduct.SingleOrDefault().vat_guid); . I have updated question to include controller code. – LavsTo Jun 09 '17 at 11:01
  • 1
    Build your SelectList using `db.q_vat.ToList().Select(x => new SelectListItem{ Value = x.vat_guid, Text = x.q_rate.ToString("0:0.#") });` –  Jun 09 '17 at 11:06
  • How do I update the line in the view, after building the list your way I am now getting following error "There is no ViewData item of type 'IEnumerable' that has the key 'vat_guid'." – LavsTo Jun 09 '17 at 11:26
  • Of all the possible ways to generate a ` –  Jun 09 '17 at 11:28
  • I will have a look at that. Cheers! – LavsTo Jun 09 '17 at 13:06

1 Answers1

0

Try the following solution

[DisplayFormat(DataFormatString = "{0:0.0}",  ApplyFormatInEditMode = true)]
public Guid? vat_guid { get; set; }
arun thatham
  • 500
  • 1
  • 4
  • 13