none
returning null value from populated selectlist with DB data RRS feed

  • Question

  • I'm implementing asp.net core MVC project. In my controller class called ApiApplicant, Create method, I have 3 selectlists that its items should be populated from a table called APIApplicantHistory. My models and create method and view are implemented like following:


            using System.Collections.Generic;
        
              namespace CSDDashboard.Models
             {
              public partial class Apiapplicant
              {
                public Apiapplicant()
                {
                    ApiApplicantHistory = new HashSet<ApiApplicantHistory>();
                }
        
                public int Id { get; set; }
                public string ApiRequestDate { get; set; }
                public int? ApiRequestNo { get; set; }
                public int? Apiid { get; set; }
                public int? ApplicantId { get; set; }
                public int? GateId { get; set; }
                public string NocRequestDate { get; set; }
                public string NocRequestNo { get; set; }
                public string Url { get; set; }
                public string Description { get; set; }
                public bool? IsDeleted { get; set; }
        
                public virtual Api Api { get; set; }
                public virtual Applicant Applicant { get; set; }
                public virtual Gate Gate { get; set; }
                public virtual ICollection<ApiApplicantHistory> ApiApplicantHistory { get; set; }
            }
        }
        
             using System;
             using System.Collections.Generic;
            
             namespace CSDDashboard.Models
             {
             public partial class ApiApplicantHistory
             {
                public int Id { get; set; }
                public int? ApiApplicantId { get; set; }
                public string Date { get; set; }
                public int? SentResponseType { get; set; }
                public int? UnconfirmedReason { get; set; }
                public int LastReqStatus { get; set; }
                public string Description { get; set; }
            
                   public virtual Apiapplicant ApiApplicant { get; set; }
                   public virtual EntityType LastReqStatusNavigation { get; set; }
                   public virtual EntityType SentResponseTypeNavigation { get; set; }
                   public virtual EntityType UnconfirmedReasonNavigation { get; set; }
                }
               }


               using System;
               using System.Collections.Generic;
        
               namespace CSDDashboard.Models
               {
                public partial class EntityType
               {
                public EntityType()
                {
                    ApiApplicantHistoryLastReqStatusNavigation = new HashSet<ApiApplicantHistory>();
                    ApiApplicantHistorySentResponseTypeNavigation = new HashSet<ApiApplicantHistory>();
                    ApiApplicantHistoryUnconfirmedReasonNavigation = new HashSet<ApiApplicantHistory>();
                }
        
                public int Id { get; set; }
                public string Name { get; set; }
                public string EntityKey { get; set; }
        
                public virtual ICollection<ApiApplicantHistory> ApiApplicantHistoryLastReqStatusNavigation { get; set; }
                public virtual ICollection<ApiApplicantHistory> ApiApplicantHistorySentResponseTypeNavigation { get; set; }
                public virtual ICollection<ApiApplicantHistory> ApiApplicantHistoryUnconfirmedReasonNavigation { get; set; }
               
            }
        }
        
        
              using System;
              using System.Collections.Generic;
              using System.Linq;
              using System.Threading.Tasks;
        
              namespace CSDDashboard.Models
              {
              public class APIApplicantViewModel
              {
                    public Apiapplicant apiApplicantvm { get; set; }
               
                     public ApiApplicantHistory apiApplicantHistoryvm { get; set; }
               }
              }
        
        
               public class ApiapplicantsController : Controller
               {
                private readonly CSSDDashboardContext _context;
        
                public ApiapplicantsController(CSSDDashboardContext context)
                {
                    _context = context;
                }
        
               public IActionResult Create()
                {
                   
                     ViewData["sentResponseType"] = new SelectList(_context.EntityType.Where(g => g.EntityKey == "sentResponseType")
                        .Include(x => x.ApiApplicantHistoryLastReqStatusNavigation).ToList(), "ID", "name");
                    ViewData["unconfirmedReason"] = new SelectList(_context.EntityType.Where(g => g.EntityKey == "unconfirmedReason").ToList(), "ID", "name");
                    ViewData["lastReqStatus"] = new SelectList(_context.EntityType.Where(g => g.EntityKey == "lastRequestStatus").ToList(), "ID", "name");
        
                    return View();
        
                }
               }

    And a part of create view implementation:

     

             @model CSDDashboard.Models.APIApplicantViewModel
        
               @{
               ViewData["Title"] = "create";
               }
                <div class="row">
                 <div class="col-md-4">
                  <form asp-action="Create">
                    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                  <div class="form-group">
                        <label asp-for="apiApplicantvm.GateId" class="control-label"></label>
                        <select asp-for="apiApplicantvm.GateId" class="form-control" asp-items="ViewBag.GateId"></select>
                    </div>
                <div class="form-group">
            <label asp-for="apiApplicantHistoryvm.SentResponseType" class="control-label"></label>
                <select asp-for="apiApplicantHistoryvm.SentResponseType" class="form-control" asp-items="ViewBag.sentResponseType"></select>
                </div>
               <div class="form-group">
                <label asp-for="apiApplicantHistoryvm.UnconfirmedReason" class="control-label"></label>
                <select asp-for="apiApplicantHistoryvm.UnconfirmedReason" class="form-control" asp-items="ViewBag.unconfirmedReason"></select>
        
               </div>
                <div class="form-group">
                    <label asp-for="apiApplicantHistoryvm.LastReqStatus" class="control-label"></label>
                    <select asp-for="apiApplicantHistoryvm.LastReqStatus" class="form-control" asp-items="ViewBag.lastReqStatus"></select>
                </div>
        <div class="form-group">
                            <input type="submit" value="Create" class="btn btn-primary" />
                        </div>
                </form>
               </div>
              </div>
        
               <div>
               <a asp-action="Index">Back</a>
               </div>
        
             @section Scripts {
              @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
            }

    In create method, all the viewData are filled with the correct related data but the problem is existing in Create view, after running the project an error like below is shown in Create page:

    An unhandled exception occurred while processing the request.
    NullReferenceException: Object reference not set to an instance of an object.

    After debugging the code I understand that In create view, apiApplicantvm is not null but apiApplicantHistoryvm returns null and the above error is because of that. I appreciate if anyone could tells me how to fix the problem.
    Saturday, February 22, 2020 8:22 AM

All replies

  • Hi Elenora.rezaie,

    Thank you for posting here.

    According to your description, your issue is more related to ASP.NET Core. Therefore, it will be more appropriate to ask your question in ASP.NET Core Forums

    The CLR Forum discuss and ask questions about .NET Framework Base Classes (BCL) such as Collections, I/O, Regigistry, Globalization, Reflection. Also discuss all the other Microsoft libraries that are built on or extend the .NET Framework, including Managed Extensibility Framework (MEF), Charting Controls, CardSpace, Windows Identity Foundation (WIF), Point of Sale (POS), Transactions.

    Thank you for your understanding.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 26, 2020 3:34 AM