Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I debug mvc4 razor views?

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

like image 439
Pakk Avatar asked Jun 24 '13 17:06

Pakk


People also ask

How do you debug a view?

Simply execute the DebugView program file (dbgview.exe) and DebugView will immediately start capturing debug output. Note that if you run DebugView on Windows 2000/XP you must have administrative privilege to view kernel-mode debug output.

How do you debug a view bag?

Click your ViewBag. MyData and hit F9 . When debugging, once the break-point is hit, hover your mouse over the ViewBag to see its contents.


1 Answers

You need to put breakpoints in your view itself. You can place breakpoints on anything using razor syntax such as:

@Html.ActionLink
@{ var x = model.x; }

If you are getting a null reference exception, put breakpoints on places where you consume the model in your view.

like image 168
David L Avatar answered Oct 13 '22 00:10

David L