I'm used to C# and vb.net winforms, and usually can find all the errors I need just by setting a breakpoint and stepping through my code.
I would like to know what I'm doing wrong.
I'm placing a breakpoint here:
public ActionResult Index(int id)
{
var cnty = from r in db.Clients
where r.ClientID == id
select r;
if (cnty != null) // breakpoint here
{
return View(cnty); // F11 jumps over this section of code returning me to the error page below.
}
return HttpNotFound();
}
Yet again I have no clue where or why it errored out exactly. How can I find out why or better yet what error it is throwing?
I'm using VS2012 mvc4 c#.
It would help to see the exception you are seeing. I'm guessing that you are seeing an exception when the page renders. As "David L" identified above, you want to set your breakpoint in the Razor view (
Index.cshtml
).But why?
It helps to understand the lifecycle of a request/response in MVC. Here is the first example I found with a visual. There are sure to be others.
ActionResult
:return View(cnty);
ActionResult
to your ViewIndex.cshtml
when attempting to use theActionResult
.I'm going to speculate that it has something to do with a disposed DB context object. Depending on the ORM you are using, the result of
is an
IQueryable<Client>
. You may be surprised to find out that your code has not yet contacted the database beforereturn View(cnty);
is executed. Try this instead:Again, the exact error you are seeing is important. My suggestion presumes
Index.cshtml
begins with:Update:
Per OP's comment below, the stack trace is not available. There are many questions dedicated to seeing the stack trace in your browser during development. At least confirm that the following is set in your
web.config
You need to put breakpoints in your view itself. You can place breakpoints on anything using razor syntax such as:
If you are getting a null reference exception, put breakpoints on places where you consume the model in your view.
First, use a
try
block. Your exception will be available in the catch block for inspection, reporting, etc.