Please use the Code Sample
button in the editor to post code. Screenshots of code do not help us as we cannot copy the code to try it ourselves. Thanks.
While you provided the code, you didn't provide us the context of when you're getting this error. So we're guessing at what you're doing to get here.
One possible scenario (at least with older versions of VS) is that you're in the designer on the cshtml file. You tell the debugger to run using F5. It brings up the browser and you get the error. This was an odd behavior of the debugger for quite a while. The workaround was to ensure that you didn't have a cshtml file active when you start the debugger. Just open a .cs file in the editor and try again.
If that is not your case then my next guess is that your GetVolunteerData
method isn't returning back anything. Since it is partial screenshot we have no way of knowing. Put a breakpoint on your return View(volunteers)
line in Index
and run the debugger. When it hits that line check the value of volunteers
. If it is null then the issue is with the method.
If you don't hit that breakpoint then this controller isn't actually being used. You have another controller that is being called instead. Unfortunately we have no way of knowing what that is. My first recommendation is to ensure your code is compiling. If you are getting any compiler errors then the debugger isn't running the code you have. Fix the errors. If it does compile then look at the call stack when the exception is thrown. I suspect it isn't getting called from where you think it is.
Also be aware that models in MVC need to have a default constructor (in addition to any other constructor you define). If they don't then you cannot use them to pass data back from the browser to the server. The runtime cannot create an instance of the model and therefore will fail the call.