0

I have created a web page and it displays a data entering form. Its first element is a textbox and it will ask to enter ID of mother. After entering the Id it will post to the server to search is mother exist or not. If exist it returns data to the view and displays them on the form. I coded it according to my logic as follows. But when I run the page it raises an error "Object reference not set to an instance of an object" from the View.cshtml page.

Controller.cs

public IActionResult Index(string id)
{
    if (!string.IsNullOrEmpty(id))
    {
        SearchData(id); //This is asked to search mother
    }
    return View();
}

public void SearchData(string id)
{
    using (_context)
    {
        if (!string.IsNullOrEmpty(id))
        {                    
            var Mother = _context.Mothers.Where(p => p.NIC == id).ToList();                   
            if (Mother.Any())
            {                        
                ViewData["Mother"] = Mother;
            }
        }
    }
}

Index.cshtml

 @{ var m = ViewData["Mother"] as School_Mgt.Models.Students.Mother; }

 <input asp-for="NICM" name="NICM" placeholder="NIC No." type="text" value="@m.NIC">  
 <input asp-for="FNameM" name="FNameM" placeholder="Name in full" type="text" value="@m.FName">
  ...                                                                                     

Rahul Sharma
  • 7,768
  • 2
  • 28
  • 54
chandrasiri
  • 83
  • 1
  • 3
  • 11
  • 1
    What happens if `id` is empty? Check on your `View`: `if(ViewData["Mother"] !=null){//your logic here}`; – Rahul Sharma Apr 08 '20 at 12:32
  • You should debug your controller and see what is returned as "Mother". Is it null? Is it of a different type? – Oguz Ozgul Apr 08 '20 at 12:32
  • ViewData["Mother"] is not empty. I checked. If you please see this screen capture. http://prntscr.com/rvhp42 – chandrasiri Apr 08 '20 at 14:58
  • After executing this code line @{ var m = ViewData["Mother"] as School_Mgt.Models.Students.Mother; } m is null which is not set to an object. Please see this screen capturehttp://prntscr.com/rvhvyl – chandrasiri Apr 08 '20 at 15:08
  • It works. Thank you very much. By changing var Mother = _context.Mothers.Where(p => p.NIC == id).ToList(); to var Mother = _context.Mothers.Where(p => p.NIC == id).FirstOrDefault(); – chandrasiri just now Edit – chandrasiri Apr 08 '20 at 15:17

1 Answers1

1

In ViewData["Mother"] = Mother; you are getting data in list of data you can try

 var Mother = _context.Mothers.Where(p => p.NIC == id).FirstOrDefault();

if you are trying to work with single data else

   @{ var m = ViewData["Mother"] as IEnumerable<School_Mgt.Models.Students.Mother;>} 

if you are trying to work with multiple data.

Rupak
  • 409
  • 1
  • 3
  • 18
  • After executing this code line @{ var m = ViewData["Mother"] as School_Mgt.Models.Students.Mother; } m is null which is not set to an object. Please see this screen capturehttp://prntscr.com/rvhvyl – – chandrasiri Apr 08 '20 at 15:10
  • It works. Thank you very much. By changing var Mother = _context.Mothers.Where(p => p.NIC == id).ToList(); to var Mother = _context.Mothers.Where(p => p.NIC == id).FirstOrDefault(); – chandrasiri Apr 08 '20 at 15:17