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">
...