0

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.

Rex B
  • 43
  • 8
  • Have you debugged the response from the controller before it enters the razor engine to ensure that you actually have properly populated the data? – David L Jun 13 '18 at 19:53
  • No, I haven't done that yet. Thank you. – Rex B Jun 13 '18 at 20:06
  • No I hadn't done that. Thanks. I'm not quite sure how to debug the response from the controller. I'm going to add the controller code for this SelectList and the ApplicationDBContext and ask how to ensure that the data is properly populated. I populated the table separately for the SelectList. – Rex B Jun 13 '18 at 21:06
  • Your code looks fine. When are you getting this exception? Is it for the first GET request or after you submit the form ? – Shyju Jun 13 '18 at 23:20
  • I get the exception on the line: 'asp-items="@(new SelectList(@ViewBag.ListofIdentifier, "AlertIndex", "Alert_Identifier"))">' in the view page AlertSelect cshtml shown above. – Rex B Jun 13 '18 at 23:34
  • Your code is fine. I just copied and tried in a local project and it worked fine. – Shyju Jun 14 '18 at 00:27
  • Just btw, the error for me comes before the page is displayed. I'll keep after it. Thanks. – Rex B Jun 14 '18 at 02:29

0 Answers0