ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 277.9k

When click next Button get error NullReferenceException

Jan 7 2019 12:22 PM
Problem
When click next Button of EmployeeId i get error on line below :
  1. <button id="BtnNext"onclick="location.href='@Url.Action("Next", "Employees",new {id=Model.EmployeeId })'" style="display:inline"><b>Next</b></button>  
NullReferenceException: Object reference not set to an instance of an object.
the line error found on view create of action create of Employees Controller .
Details
 
I have Employees controller have action Create and his view Create
ON View Create I put button Next to show next Row EmployeeId,EmployeeName,etc
but it show error above
Next button it must when click on it show next record and related data if exist
technology used asp.net core 2.1 visual studio 2017 sql server 2012
  1. Employee Controller Code  
  2.   
  3.    
  4.   
  5.    
  6.   
  7.     public class EmployeesController : Controller  
  8.         {  
  9.             private readonly IEmployees _context;  
  10.       
  11.             public EmployeesController(IEmployees context)  
  12.             {  
  13.                 _context = context;  
  14.             }  
  15.             public IActionResult Create()  
  16.             {  
  17.                return View(model);  
  18.             }  
  19.            [HttpPost]  
  20.             [ValidateAntiForgeryToken]  
  21.             public async Task<IActionResult> Create(Employee employee)  
  22.             {  
  23.             return View(employee);  
  24.             }  
  25.             public ActionResult Next(int id)  
  26.             {  
  27.                 var nextID = _context.GetAll().OrderBy(i => i.EmployeeId)  
  28.                          .SkipWhile(i => i.EmployeeId != id)  
  29.                          .Skip(1)  
  30.                          .Select(i => i.EmployeeId);  
  31.                 ViewBag.NextID = nextID;  
  32.                
  33.                 return View("Create");  
  34.             }  
  35.       
  36.         
  37.   
  38.          <div class="row">  
  39.                 <div class="col-md-4">  
  40.     <button id="BtnNext"onclick="location.href='@Url.Action("Next", "Employees",new {id=Model.EmployeeId })'" style="display:inline"><b>Next</b></button>  
  41.                  <form asp-action="Create">  
  42.           
  43.                     <div asp-validation-summary="ModelOnly" class="text-danger">   
  44.               </div>  
  45.                     <div class="form-group">  
  46.                         <label asp-for="EmployeeId" class="control-label"></label>  
  47.                         <input asp-for="EmployeeId" class="form-control" />  
  48.                         <span asp-validation-for="EmployeeId" class="text-danger"></span>  
  49.                     </div>  
  50.                     <div class="form-group">  
  51.                         <label asp-for="BranchCode" class="control-label"></label>  
  52.                         <input asp-for="BranchCode" class="form-control" />  
  53.                         <span asp-validation-for="BranchCode" class="text-danger"></span>  
  54.                     </div>  
  55.                     <div class="form-group">  
  56.                         <label asp-for="EmployeeName" class="control-label"></label>  
  57.                         <input asp-for="EmployeeName" class="form-control" />  
  58.                         <span asp-validation-for="EmployeeName" class="text-danger"></span>  
  59.                     </div>  
  60.                       
  61.                     <div class="form-group">  
  62.                         <input type="submit" value="Create" class="btn btn-default" />  
  63.                     </div>  
  64.                 </form>  
  65.             </div>  
  66.         </div>  
 

Answers (5)