I've seen many iterations of this problem, involving dropdownlistfor tag helper more than select tag helper (my problem) such as here and here, but I haven't found the answer that works for me yet, so I'm looking for help.
The line of code that is highlighted in the stacktrace is from my file AlertSelect.cshtml:
asp-items="@(new SelectList(@ViewBag.ListofIdentifier, "AlertIndex", "Alert_Identifier"))"></select>
@model edxl_cap_v1_2.Models.EdxlCapMsg
@{
<form asp-controller="EdxlCapMsg" asp-action="Index" method="post"
class="form-horizontal" role="form">
<div class="form-group">
<div class="alert-danger" asp-validation-summary="ModelOnly"></div>
<div class="content-wrapper">
<span class="smallText">
<label asp-for="Alert_Identifier" class="control-label"> </label>
<select asp-for="AlertIndex"
class="form-control"
asp-items="@(new SelectList(@ViewBag.ListofIdentifier, "AlertIndex", "Alert_Identifier"))"></select>
</span>
</div>
</div>
@*<div class="form-group">
<div class="content-wrapper">
<input id="Submit1" type="submit" value="submit" />
</div>
</div>
<div class="form-group">
<div class="content-wrapper">
@if (ViewBag.SelectedValue != null)
{
<text>Selected Alert_Identifier: </text> @ViewBag.SelectedValue;
}
</div>
</div>*@
</form>
}
'items' references the AlertIndex and Alert_Identifier columns of the EdxlCapMsg table in the MySql databsase, which is prepopulated with 3 records. I'm really stumped because I created this dropdownlist from a tutorial in a separate solution in Visual Studio 2017 with an identical table in another MySql database and it works okay up to this point. (U can see where I've commented out the code that will be adapted to using the selection from the dropdownlist to select the record for editing in the much larger application.)
Since the table is populated, I'm not seeing how the value could be null before it has even been displayed.
Thanks in advance. I'm sure this is a dumb mistake, but I'm not seeing it.
Here is the controller:
public class EdxlCapMsgController : Controller
{
private readonly ApplicationDbContext _context;
public EdxlCapMsgController(ApplicationDbContext context)
{
_context = context;
}
// GET: EdxlCapMessages
public IActionResult Index()
{
List<EdxlCapMsg> identifierlist = new List<EdxlCapMsg>();
//------Getting Data fom Database using EntityFrameworkCore------
identifierlist = (from product in _context.EdxlCapMsg
select product).ToList();
//------Inserting Select Item in List------
identifierlist.Insert(0, new EdxlCapMsg { AlertIndex = 0, Alert_Identifier = "Select" });
//------Assigning identifierlist to ViewBag.ListofIdentifier------
ViewBag.ListofIdentifier = identifierlist;
return View();
}
[HttpPost]
public IActionResult Index(EdxlCapMsg EdxlCapMsg)
{
//------Validation------
if (EdxlCapMsg.AlertIndex == 0)
{
ModelState.AddModelError("", "Select EdxlCapMsg");
}
//------Getting selected value------
int SelectedValue = EdxlCapMsg.AlertIndex;
ViewBag.SelectedValue = EdxlCapMsg.AlertIndex;
//------Setting Data back to ViewBag after Posting form------
List<EdxlCapMsg> identifierlist = new List<Models.EdxlCapMsg>();
identifierlist = (from product in _context.EdxlCapMsg
select product).ToList();
identifierlist.Insert(0, new EdxlCapMsg { AlertIndex = 0, Alert_Identifier = "Select" });
ViewBag.ListofIdentifier = identifierlist;
return View();
}
}
}
Here is the ApplicationDbContext:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
this.Database.EnsureCreated();
}
public DbSet<EdxlCapMsg> EdxlCapMsg { get; set; }
public virtual DbSet<Person> Persons { get; set; }
public DbSet<Alert> Alert { get; set; }
public DbSet<Area> Area { get; set; }
public DbSet<DataCategory> DataCategory { get; set; }
public DbSet<Element> Element { get; set; }
public DbSet<Info> Info { get; set; }
public DbSet<Resource> Resource { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
....
For the SelectList the EdxlCapMsg is the major significant class.