The value in the database is 6, datatype number(4,1), using Oracle database.
When I try to click the record for edit, the view is displaying "------ Select--------" instead of the value of the Glove Size 6.0. I think the value retrieved from the database 6 is not equal to 6.0 as one of the items in the dropdownlist, that's why its
not showing. How do I go about it?
Member
44 Points
279 Posts
Display a Decimal in DropDownList
Oct 09, 2019 05:35 PM|tinac99|LINK
Hi,
I have a decimal column GloveSize. Input entry is a dropdownlist which I populated using the following:
Model
=================================
[Column("GLOVESIZE")]
[Display(Name = "Glove Size")]
[DisplayFormat(DataFormatString = "{0:##.0}", ApplyFormatInEditMode = true)]
public decimal? GloveSize { get; set; }
Controller
=====================
public static List<string> getGlovesSizes()
{
List<string> list = new List<string>();
list.Add("6.0");
list.Add("6.5");
list.Add("7.0");
list.Add("7.5");
list.Add("8.0");
list.Add("8.5");
list.Add("9.0");
list.Add("9.5");
list.Add("10.0");
list.Add("10.5");
list.Add("11.0");
list.Add("11.5");
list.Add("12.0");
list.Add("12.5");
list.Add("13.0");
return list;
}
private void populateGlovesSize()
{
List<decimal> _glovesSizeList = new List<decimal>();
ViewBag.GlovesSizeList = Enumerations.getGlovesSizes();
}
View
==============
<div class="form-group">
<label asp-for="User.GloveSize"></label>
@Html.DropDownListFor(m => Model.User.GloveSize,
new SelectList(ViewBag.GlovesSizeList), " ----- Select ------")
<span asp-validation-for="User.GloveSize" class="text-danger"></span>
</div>
The value in the database is 6, datatype number(4,1), using Oracle database.
When I try to click the record for edit, the view is displaying "------ Select--------" instead of the value of the Glove Size 6.0. I think the value retrieved from the database 6 is not equal to 6.0 as one of the items in the dropdownlist, that's why its not showing. How do I go about it?
Thanks,
tinac99
All-Star
43801 Points
18760 Posts
Re: Display a Decimal in DropDownList
Oct 09, 2019 05:51 PM|mgebhard|LINK
I don't see any place in the shared code where a selected value is assigned. I recommend using the standard List<SelectListItem>.
The dropdownlist has an overload that allow you to set the selected value.