I have an MVC project, i am having trouble populating a dropdown list within a view.
I am successfully populating a DataModel from a table in my Entity Framework. But the trouble is moving the values into the SelectListItem.
Here is my Controller code.
using (ASP_AuthEntities ReporList = new ASP_AuthEntities())
{
var DataModel = new SelectList(ReporList.Report_Security.ToList(),"ReportName", "Report_Url");
ViewData["Reports"] = DataModel;
}
and here is my cshtml piece to populate the SelectListItem.
Please take a fee seconds to set a break point and debug your code. This code block looks very suspicious as you've defined the DataModel inside a sing block. The DataModel is set to be garbage collected at the end of the using block.
using (ASP_AuthEntities ReporList = new ASP_AuthEntities())
{
var DataModel = new SelectList(ReporList.Report_Security.ToList(),"ReportName", "Report_Url");
ViewData["Reports"] = DataModel;
}
Try declaring DataModel outside the using statement.
SelectList DataModel;
using (ASP_AuthEntities ReporList = new ASP_AuthEntities())
{
DataModel = new SelectList(ReporList.Report_Security.ToList(),"ReportName", "Report_Url");
}
ViewData["Reports"] = DataModel;
most likely the controller code is not being run (or the ViewData["report"] is replaced). as suggested you should use the debugger. set a breakpoint in controller and view.
This is my Controller which i have inserted a breakpoint and the code runs successfully.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FormsAuthenticationMVC.Models;
namespace FormsAuthenticationMVC.Controllers
{
//[Authorize]
public class HomeController : Controller
{
public ActionResult Reports()
{
ViewBag.Message = "O2 Dashboard";
ASP_AuthEntities ReportTable = new ASP_AuthEntities();
var getReportTable = ReportTable.Report_Security.ToList();
SelectList list = new SelectList(getReportTable, "ReportName", "Report_Url");
ViewBag.ReportListName = list;
return View();
}
}
}
Here is my reports View.
@{
<h2>@ViewBag.Message</h2>
}
<h2>Reports</h2>
@Html.DropDownList("ReportsList", ViewBag.ReportsList as SelectList, "Select Report")
as soon as the debugger hits the @Html.DropDownList.... line i get the same error.
System.InvalidOperationException: 'There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'ReportsList'.'
I can see the controller code is getting all values from the table within the Entity Framework Model.
public ActionResult Index()
{
List<Report> getReportTable = new List<Report>
{
new Report { ReportName = "Report 1", Report_Url = "URL 1" },
new Report { ReportName = "Report 2", Report_Url = "URL 2" }
};
var selectList = new List<SelectListItem>();
foreach(var report in getReportTable)
{
selectList.Add(new SelectListItem { Text = report.ReportName, Value = report.Report_Url });
}
ViewBag.ReportList = selectList;
return View();
}
Result:
Best Regards,
Jiadong Meng
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
This is a test demo and Report is my custom class for convenience, it doesn't matter. Your data may come from the database, you only need to fill the data into the Text and Value of the selectlistItem based on your needs. I can only guess since you didn't
show us your model,
public ActionResult Reports()
{
ASP_AuthEntities ReportTable = new ASP_AuthEntities(); var getReportTable = ReportTable.Report_Security.ToList();
var selectList = new List<SelectListItem>();
foreach(var report in getReportTable)
{
selectList.Add(new SelectListItem { Text = report.ReportName, Value = report.Report_Url });
}
ViewBag.ReportList = selectList;
return View();
}
If you still can't solve it, please show us your model.
Best Regards,
Jiadong Meng
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
35 Points
375 Posts
Populating SelectListItem ( Drop down List ) in MVC view
Jan 22, 2020 02:08 PM|masterdineen|LINK
Hello there.
I have an MVC project, i am having trouble populating a dropdown list within a view.
I am successfully populating a DataModel from a table in my Entity Framework. But the trouble is moving the values into the SelectListItem.
Here is my Controller code.
and here is my cshtml piece to populate the SelectListItem.
now when i run, i get the following error.
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'ReportName'.
the dataview contains two columns, (ReportName, & ReportURL) with 6 rows.
ReportName being the Label
ReportURL being the Value.
i have followed the below example but cannot workout whats going on.
https://www.c-sharpcorner.com/article/different-ways-bind-the-value-to-razor-dropdownlist-in-aspnet-mvc5/
see section ( Populate a DropDownList using Database with Entity Framework )
Can someone point me in the right direction please.
Regards Rob
All-Star
53001 Points
23596 Posts
Re: Populating SelectListItem ( Drop down List ) in MVC view
Jan 22, 2020 02:32 PM|mgebhard|LINK
Please take a fee seconds to set a break point and debug your code. This code block looks very suspicious as you've defined the DataModel inside a sing block. The DataModel is set to be garbage collected at the end of the using block.
using (ASP_AuthEntities ReporList = new ASP_AuthEntities()) { var DataModel = new SelectList(ReporList.Report_Security.ToList(),"ReportName", "Report_Url"); ViewData["Reports"] = DataModel; }
Try declaring DataModel outside the using statement.
Still use standard tooling to debug your code.
All-Star
58174 Points
15647 Posts
Re: Populating SelectListItem ( Drop down List ) in MVC view
Jan 22, 2020 04:03 PM|bruce (sqlwork.com)|LINK
most likely the controller code is not being run (or the ViewData["report"] is replaced). as suggested you should use the debugger. set a breakpoint in controller and view.
Member
35 Points
375 Posts
Re: Populating SelectListItem ( Drop down List ) in MVC view
Jan 22, 2020 08:22 PM|masterdineen|LINK
Hello there.
I have used a different method.
This is my Controller which i have inserted a breakpoint and the code runs successfully.
Here is my reports View.
as soon as the debugger hits the @Html.DropDownList.... line i get the same error.
System.InvalidOperationException: 'There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'ReportsList'.'
I can see the controller code is getting all values from the table within the Entity Framework Model.
What am i doing wrong.???
All-Star
58174 Points
15647 Posts
Re: Populating SelectListItem ( Drop down List ) in MVC view
Jan 22, 2020 08:25 PM|bruce (sqlwork.com)|LINK
in the controller you used:
in the view you used:
@Html.DropDownList("ReportsList", ViewBag.ReportsList as SelectList, "Select Report")
as the names don't match, I would not expect it to work
Member
35 Points
375 Posts
Re: Populating SelectListItem ( Drop down List ) in MVC view
Jan 22, 2020 09:18 PM|masterdineen|LINK
i have just updated the @Html.DropDownList to
<li>@Html.DropDownList("ReportList", ViewBag.ReportsListName as SelectList, "Select Report")</li>
the viewbag names are matching now.
error = There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'ReportList'.'
and its still complaining about the name
All-Star
53001 Points
23596 Posts
Re: Populating SelectListItem ( Drop down List ) in MVC view
Jan 22, 2020 09:58 PM|mgebhard|LINK
Try
@Html.DropDownList("ReportName", ViewBag.ReportsList as SelectList, "Select Report")
Member
35 Points
375 Posts
Re: Populating SelectListItem ( Drop down List ) in MVC view
Jan 22, 2020 10:07 PM|masterdineen|LINK
Thank you for coming back again.
just tried
<li>@Html.DropDownList("ReportName", ViewBag.ReportsListName as SelectList, "Select Report")</li>
and
<li>@Html.DropDownList("ReportName", ViewBag.ReportsList as SelectList, "Select Report")</li>
same error
System.InvalidOperationException: 'There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'ReportName'.'
All-Star
53001 Points
23596 Posts
Re: Populating SelectListItem ( Drop down List ) in MVC view
Jan 22, 2020 10:28 PM|mgebhard|LINK
The try just...
... Or whatever you ViewBag is named.
Member
35 Points
375 Posts
Re: Populating SelectListItem ( Drop down List ) in MVC view
Jan 22, 2020 10:35 PM|masterdineen|LINK
no luck same sorry
All-Star
53001 Points
23596 Posts
Re: Populating SelectListItem ( Drop down List ) in MVC view
Jan 22, 2020 11:09 PM|mgebhard|LINK
I built a working test based on your struggles. It just works as expected. Are you sure you are using the debugger?
Participant
1320 Points
491 Posts
Re: Populating SelectListItem ( Drop down List ) in MVC view
Jan 23, 2020 06:18 AM|jiadongm|LINK
Hi masterdineen,
I made a test based on your codes, you can refer to the below example:
View:
Controller:
Result:
Best Regards,
Jiadong Meng
Member
35 Points
375 Posts
Re: Populating SelectListItem ( Drop down List ) in MVC view
Jan 23, 2020 06:25 AM|masterdineen|LINK
in your example i am getting an error with the following line.
List<Report> getReportTable = new List<Report>
The type or namespace name 'Report' could not be found (are you missing a using directive or an assembly reference?
sorry i am quite new to this.
Participant
1320 Points
491 Posts
Re: Populating SelectListItem ( Drop down List ) in MVC view
Jan 23, 2020 08:43 AM|jiadongm|LINK
Hi masterdineen,
This is a test demo and Report is my custom class for convenience, it doesn't matter. Your data may come from the database, you only need to fill the data into the Text and Value of the selectlistItem based on your needs. I can only guess since you didn't show us your model,
If you still can't solve it, please show us your model.
Best Regards,
Jiadong Meng
Member
35 Points
375 Posts
Re: Populating SelectListItem ( Drop down List ) in MVC view
Jan 23, 2020 09:18 AM|masterdineen|LINK
I have worked it out,
i was referencing
@Html.DropDownList("ReportListName", ViewBag.ReportsListName as SelectList, "Select Report")
twice in my views.
Thank you all for replying to my question.