0

My team have been working on building a comment section for a school project and render it in a partial view.

This is the solution that I came up with:

On Index page:

@Html.Partial("StatusWall", new Project.Services.StatusServices())

StatusWall:

@model Project.Services.StatusServices

@foreach (var status in Model.GetAllUserStatuses())
{...

Code in StatusService:

public List<UserStatus> GetAllUserStatuses()
    {
        //check
        var StatusResult = (from status in db.UserStatuses
                            orderby status.DateInserted descending
                            select status).ToList();
        return StatusResult;
    }

This works, but the problem is that we're not using a proper MVC architecture, since we're bypassing the controller. Since the design of our project requires that I use a partial view, I had no idea how to call the controller, until my assistant pointed out the [ChildActionOnly] attribute

Now, I'm trying to incorporate the code to a MVC model but the solution, so far, eludes me.

Here's what I've written in the Controller:

    [ChildActionOnly]
    public ActionResult StatusWall() {
        var service = new StatusServices();
        var result = service.GetAllUserStatuses();
        return PartialView(result);
    }

Index page:

@Html.Action("StatusWall")

All other code is unchanged.

This tosses the following error:

An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code

Additional information: Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.

Can anyone explain to me what I'm doing wrong?

Community
  • 1
  • 1

1 Answers1

2

I realized what was wrong.

I changed...

@model Project.Services.StatusServices

@foreach (var status in Model.GetAllUserStatuses())
{...

...into...

@model List<Project.Models.UserStatus>

@foreach (var status in Model)
{...

There was also some minor fixing, like turning ActionResult into PartialViewResult, but I don't know if that was necessary for the code to work.