-2

In _Layout.cshtml, I have this part of code

<div>
      @Html.Action("BestStudent", "Student")
</div>

In Student Controller I have created BestStudent method as below

public class StudentController : Controller
{
    private UniversityInitial dbAllStudents  = new UniversityInitial();

    [ChildActionOnly]
    public ActionResult BestStudent()
    {
        var best = dbAllStudents.Students
            .Where(s => s.LastName.StartsWith("M")&s.FirstName.StartsWith("E") & s.City.StartsWith("T"));
        return PartialView("_BestStudent", best);
    }
}

In Views/Shared folder I have created the Partial View named _BestStudent.cshtml strongly typed for Student model as below:

@model UniversityApp.Models.Student

<h3 class="text-danger">Student of the year</h3>
Name : @Model.FirstName
Last Name : @Model.LastName
City: @Model.City

Build succeed but when I run it I got one exception like:

"Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil...

and the breakpoint is to the line of code below:

<div>
    @Html.Action("BestStudent", "Student")
</div>

So I guess something wrong happens here, but I can not figure it out what

Dea
  • 291
  • 3
  • 6
  • 17

1 Answers1

1

Update

Fix this Query like below:

var best = dbAllStudents.Students
                        .Where(s => s.LastName.StartsWith("M")&s.FirstName.StartsWith("E") & s.City.StartsWith("T")).FirstOrDefault();
return PartialView("_BestStudent", best);

Credit Goes to @StephenMuecke

Masoud Andalibi
  • 3,168
  • 5
  • 18
  • 44