I have a DropDownList on my Razor page.
I am binding the DropDownList with a ViewData.
below is the code for my controller where I am getting the data from the database:
public async Task<IActionResult> Index()
{
List<DocumentType> docTypes = new List<DocumentType>();
docTypes = await _documentTypeService.GetDocumentTypes(deptId);
ViewData["DocumentType"] = new SelectList(docTypes, "Id", "Description");
}
This is how I am displaying the drop down list and I am also selecting a value for the DropDownList based on certain condition.
This is the view markup:
<table class="table table-bordered">
<thead>
<tr>
<th>Type</th>
</thead>
<tbody>
@if (Model != null)
{
@foreach (var item in Model)
{
string selected = String.Empty;
@foreach (var code in (SelectList)ViewData["DocumentType"])
{
if (code.Text.Contains("This is the test for RCA-500"))
{
selected = code.Text;
break;
}
}
<tr>
<td>
<select class="form-control" style="min-width:150px" id="ddldoc_@item.DocumentId" asp-items="@(new SelectList(ViewBag.DocumentType,selected))" asp-for="@item.DocumentType"></select>
</td>
</tr>
This drop down is displaying "Microsoft.AspNetCore.Mvc.Rendering.SelectlistItem" instead of the actual values. Not sure what am I doing wrong.
I tried removing the @
symbol from asp-items, but then I start getting a compiler error saying ";" is missing.