troubleshooting Question

ASP.NET MVC Core view renders as JSON or not called at all.

Avatar of Babak Sekandari
Babak SekandariFlag for United States of America asked on
.NET ProgrammingASP.NET
10 Comments1 Solution14 ViewsLast Modified:
I have an .NET MVC Core app.
I created a model class, and a controller called LeaveRequest.
In the LeaveRequest code, I right-clicked the Create method and created a .cshtml Razor page based on the model.
I get a Razor page wherein I can enter data fine and it saves it to the database just fine, but then the debugger steps to the Index method and the page that displays has everything in a JSON layout.
I also don't see the _Layout.cshtml. In fact, the  @RenderBody() doesn't get called when the Index method returns.
I put breakpoints in the Index.cshtml and neither the @{ ViewData[... } nor the @Html.DisplayNameFor(...
ever get hit.
The URL shows: localhostxxx/LeaveRequest after the Index method returns.
Please advise.

Edits, 6/11/2021 :

Folder structure:

Here are some LeaveRequestController.cs methods:

This gets called immediately before the browser renders the results:
public async Task<IEnumerable<LeaveRequestModel>> Index()
    var leaveReqView = await _context.LeaveRequestTable.ToListAsync();  
    return leaveReqView;

It originally looked like this:
// This gets the error:
// CS0029   Cannot implicitly convert type 'Microsoft.AspNetCore.Mvc.ViewResult' to 'WebApp_OpenIDConnect_DotNet.Models.LeaveRequestModel'   
public async Task<LeaveRequestModel> Index()
    var leaveReqView = View(await _context.LeaveRequestTable.FirstOrDefaultAsync());        
    return leaveReqView;

Here is the first Create method in LeaveRequestController.cs
public IActionResult Create()
public IActionResult Create()
    return View();

Here is the Create method that gets called after the new data is entered:
public async Task<IActionResult> Create([Bind("Title,ColleagueID,Name,Email,EmployeeID,LeaveJSON,Comments,SupervisorName,SupervisorEmail,SupervisorID,Status,Supervisor_Approval,HRName,HREmail,HR_Approval,SupervisorApprovalDate,HRApprovalDate,Manager,Location,Location_Name,Location_Location,Location_Email,Location_ID,flag,RealManager,HRELocation_Phoneail,sdoc,SupervisorComment,HRComment,SLE,EXT,VAC,Modified,Created,Created_By,Modified_By")]
    LeaveRequestModel leaveRequestModel)
    if (ModelState.IsValid)
        await _context.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
    return View(leaveRequestModel);

And here is the final output in the browser:

I did change the _Layout page code towards the bottom to this:
@RenderSection("scripts", required: false)

@*@if (IsSectionDefined("scripts"))
        @await RenderSectionAsync("Scripts", required: false)

David H.H.Lee
Digital Marketing (Hospitality)

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 10 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros