How to Solve select option error null reference exception after submit button

71 views Asked by At

I am working on a project that involved the use of the select option in ASP.Net Core 7.0, I encountered some difficulties. Specifically, I faced errors related to a null reference exception in my dropdown. However, I observed that after submitting the data, it is installed into the SQL database, upon returning after submission, a null reference exception error is displayed. These errors have made the development process quite challenging, and I'm hoping to find a solution soon. Any advice or suggestions would be greatly appreciated.

Error

enter image description here

Action

public IActionResult Apply()
{

    ViewBag.Courselist = new SelectList(GetAllAccountIDList(), "Id", 
    "CourseName");
    return View();
}
[HttpPost]
public IActionResult Apply(PurchasedFormViewModel purchasedFormVM)
{
    if (ModelState.IsValid)
    {
    try
    {
        var transaction = new StudentBiodataViewModels
        {
            CourseId = purchasedFormVM.CourseId,
            SurName = purchasedFormVM.SurName,
            OtherName = purchasedFormVM.OtherName,
            PhoneNumber = purchasedFormVM.PhoneNumber,
            Email = purchasedFormVM.Email,
        };
        _context.StudentBiodata.Add(transaction);
        _context.SaveChangesAsync();
    }
    catch (Exception ex)
    {

        ViewBag.Message = ex.Message + " Error occur while inserting records.";
    }
    }
    return View();
}

Models

 public class ApplyFormViewModel
 { 
     public int Id { get; set; }
     [Display(Name = "Course Apply")]
     [Required(ErrorMessage = "Course Apply is Required")]
     public int CourseId { get; set; }
     [Display(Name = "SurName")]
     [Required(ErrorMessage = "SurName is Required")]
     public string SurName { get; set; }
     [Display(Name = "Other Name")]
     [Required(ErrorMessage = "Other Name is Required")]
     public string OtherName { get; set; } 
     [Display(Name = "Phone Number")]
     [Required(ErrorMessage = "Phone Number is Required")]
     ErrorMessage = "Entered phone format is not valid.")]
     public string PhoneNumber { get; set; } 
     [Required(ErrorMessage = "Email is Required")]
     public string Email { get; set; }
     
 }

View

<select id="fundcode" asp-for="CourseId" class="form-control">
    <option value="">--Please select--</option>
    @foreach (var item in ViewBag.Courselist )
    {
        <option value="@item.Value">@item.Text</option>
    }
</select>
1

There are 1 answers

0
Moccaccino On

You are returning from the POST to the same view called in the GET handler, but without setting the ViewBag.CourseList. Of course when the page is rendered that variable is NULL and you get the exception.

I think you need to add the same code used in the GET just before returning to the View inside the Post handler

[HttpPost]
public IActionResult Apply(PurchasedFormViewModel purchasedFormVM)
{
    .....
    ViewBag.Courselist = new SelectList(GetAllAccountIDList(), "Id", "CourseName");
    return View();
}