I’m new to .Net 8 and I’ve build a form using MVC, EF and X.PagedList. My form has a multi-select drop-down list. I’m able to save the form to database. The Multi-select drop-down list is saved to database like this [“abc”, “xyz”].
Here is my Model:
public partial class Bat
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public IEnumerable<string>? MyMultiSelectList { get; set; } = null!;
}
But when I go to view Index page, I get error at IQueryable line below –
ArgumentException: Expression of type ‘System.Collections.Generic.IEnumerable
1[System.String]' cannot be used for parameter of type 'System.Collections.Generic.IList
1[System.String]’ of method ‘System.Collections.Generic.IList1[System.String] PopulateList[String](System.Collections.Generic.IList
1[System.String], System.Collections.Generic.IList`1[System.String])’ (Parameter ‘arg0’).
Here is my code of Index method
public IActionResult Index(int? page)
{
IQueryable<Bat> Bats = _context.Bats.AsQueryable();
int pageSize = 10;
int pageNumber = (page ?? 1);
return View(Bats.ToPagedList(pageNumber, pageSize));
}
Here is the code of my Index page
@using X.PagedList
@using X.PagedList.Mvc.Core
@model X.PagedList.IPagedList<QII.Models.Initiative>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.MyMultiSelectList)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.MyMultiSelectList)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>
Can someone tell me how to display this multi-select drop-down list as string like abc,xyz on my Index page and get rid of this error?