I have an int property for Age and a list of selectable possible ages to choose from. When I receive data for editing, I get an error when adding the tostring () method, which says:
Templates can only be used with access to a field, access to properties, an index of a one-dimensional array, or one-parameter expressions of a custom indexer.
so my view model looks like this:
public int LowerAgeLimit { get; set; }
public List<SelectListItem> Ages { get; set; }
My query retrieves the data correctly, and then in the view, I try to display the data as follows:
@Html.DropDownListFor(x => x.LowerAgeLimit.ToString()
, Model.CreateGroupForm.Ages
, new { @class = "form-control" })
Without the tostring () method, it displays an empty value, which makes sense since it expects a string. But this will not allow me to convert the string without error. I also tried to create a new Age Age property in the viewmodel and convert the int to a string in the request, but I still cannot get a drop-down list to display the value.
In the request, I populated a list of ages as follows:
model.CreateGroupForm.Ages.Insert(0, (new SelectListItem { Text = "", Value = "" }));
for (int i = 1; i < 100; i++)
{
model.CreateGroupForm.Ages.Insert(i, (new SelectListItem {Text = i.ToString(), Value = i.ToString()}));
}
source
share