System.NullReferenceException while trying to load a view

Azhar Qureshi 21 Reputation points
2021-11-10T08:28:33.963+00:00

Complete Error Message :
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Derawala.Views
StackTrace:
at AspNetCore.Views_Admin_DataMgmt.<<ExecuteAsync>b__30_0>d.MoveNext() in D:\ASP.Net Core Projects\Derawala\Views\Admin\DataMgmt.cshtml:line 33

I am trying to load DataMgmt.cshtml view by clicking on a link from my _Layout page but the page which should be loaded throws the above mentioned error.
Actually the button which throws this error is on DataMgmt.cshtml page. Without pressing that button, how could that button throw the NullReferenceException .

I am uploading the files and the error screeshot :
148116-screenshot-91.png

DataMgmt.cshtml - view file

@model Derawala.Models.AdminDataMgmt  
@{  
    ViewData["Title"] = "DataMgmt";  
    Layout = "_Layout";  
}  
<form method="post">  
    <input type="hidden" asp-for="Category" />  
    <input type="hidden" asp-for="Operation" />  
    <h1 class="text-success">Data Management</h1>  
    <hr class="mx-n3" />  
    <div class="row align-items-center py-3">  
        <div class="col-md-8 pe-5">  
            <select asp-for="Category" id="CType" class="form-select form-select-lg mb-3" aria-label=".form-select-lg example">  
                <option selected disabled>--Select A Category To Manage--</option>  
                <option value="State">State</option>  
                <option value="Bank">Bank</option>  
                <option value="Qualification">Qualification</option>  
                <option value="Board">Education Board</option>  
  
            </select>  
            <span asp-validation-for="Category" class="text-danger"></span>  
        </div>  
        <div class="col-md-4 pe-5">  
            <select asp-for="Operation" id="OType" class="form-select form-select-lg mb-3" aria-label=".form-select-lg example">  
                <option selected disabled>--Select Operation Type--</option>  
                <option value="Add">Add</option>  
                <option value="Remove">Remove</option>  
  
            </select>  
            <span asp-validation-for="Operation" class="text-danger"></span>  
        </div>  
        <div>  
             <button type="submit" asp-all-route-data="@(new Dictionary<string, string> { {"c",Model.Category},{"o",Model.Operation } })" class="btn btn-primary btn-lg m-3" id="gobtn">Go!</button>  
        </div>  
    </div>   
    <div>  
        <partial name="@ViewBag.PartialView" />  
    </div>  
</form>  

AdminController.cs File :

 public IActionResult DataMgmt()  
        {            
             ViewBag.PartialView = "_DefaultDataMgmt";           
             return View();  
        }  
  
  
        [HttpPost]  
        [ValidateAntiForgeryToken]  
        public IActionResult DataMgmt(string c,string o)  
        {  
            if (c == "State" && o== "Add")  
            {  
                ViewBag.PartialView = "_StatePartial";  
            }  
            else  
            {  
                ViewBag.PartialView = "_DefaultDataMgmt";  
            }  
  
            return View();  
        }  



  
  
  
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,540 questions
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,316 Reputation points Microsoft Vendor
    2021-11-11T05:57:32.027+00:00

    Hi @Azhar Qureshi ,

    The issue is clearly that you didn't pass the AdminDataMgmt model to the view.

    To solve this issue, you could create an AdminDataMgmt instance and set the default value for the Category and Operation property, then return it to the view, code as below:

        public IActionResult DataMgmt()  
        {  
            ViewBag.PartialView = "_DefaultDataMgmt";  
    
            var admindatamgmt = new AdminDataMgmt();  
            admindatamgmt.Category = "default value";  
            admindatamgmt.Operation = "default value";  
    
            return View(admindatamgmt);  
        }  
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.