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..

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top