You would actually use c.ArticleID.ToString(). The reason is that you're concatenating a string together, therefore need string values to do so. Value and Text are both string types so it makes sense that you need strings.
mvc3 INT in a IEnumerable selectlistitem
-
23-03-2022 - |
Question
I have a selectlistitem that gathers data from the database and inserts it into a dropdownlist. All of that works but I can't seem to make the INT work for instance if I write this.. etc. The issue is with the ArticleID
IEnumerable<SelectListItem> items = db.Articles.Where(c=>c.RegistrationID==getid)
.Select(c => new SelectListItem
{
Value =c.ArticleID +"/"+ c.title,
Text = c.ArticleID + "/" + c.title
});
ViewBag.articles = items;
I get the error Unable to cast the type 'System.Int32' to type 'System.Object' and then my dropdownlist gets highlighted which is
@Html.DropDownList("article1",(IEnumerable<SelectListItem>)ViewBag.articles)
I obviously can't do c.ArticleID.Tostring() , so how can I fix this issue ? If I take the ArticleID out everything works correctly but I need that in there..
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow