Search code examples
asp.net-mvcasp.net-mvc-5partial-viewschild-actions

Understanding the process of loading a ChildAction


This is for a recruitment website: I have a form which displays the job and there is an Apply button. If user click on Apply a pop-up will open, where user clicks his info and clicks on Send button:

enter image description here

When user clicks on Apply a modal will open... this modal contains user information, i.e. name and his CV (we keep a copy of use CV in DB from prev job applications):

enter image description here

Now, I want to make this an efficient design... I don't want to load user CV into the View to begin with, to improve the load time... so I am thinking of putting the whole Modal in a ChildAction as below:

DisplayJob.cshtml

@model JobViewModel
@Html.DisplayFor(m => m.JobDescription);
<input type="button" value="Apply" />
@Html.Action("ApplyForJobModal", new { /* route params */ }); // modal to apply for job

Controller Actions:

public class JobController : Controller
{
    public ActionResult DisplayJob()
    {
        var jobViewModel = <-- Get JobViewModel from DB
        return View(jobViewModel);
    }

    [ChildActionOnly]
    public ActionResult ApplyForJobModal(/* route params */)
    {
        var userInfoViewModel = <-- Get user name from Claims and CV from DB
        return PartialView("_ApplyForJobModal", userInfoViewModel);
    }
}

I am trying to understand how the ChildAction PartialView is loaded:

  1. Does the client sends 2 requests to the server, one for loading the view and another for loading the ChildAction? Or everything is sent back in one single request?
  2. If 2 requests are sent to the server, is the display of the main view delayed until the child is loaded?
  3. If the view is delayed until child action is loaded, would it be more efficient to load the modal using ajax, which lets the page to display and load the modal behind the scene?

Solution

  • I inspected the code using Chrome Network tab:

    enter image description here

    Call to View and PartialView (Action and ChildAction) are part of the same HttpRequest, which means displaying of the view is delayed until ChildAction is ready and then the entire result it sent to the client as part of one single response.

    Another point to mention is that the Constructor of JobController was called 2 times, which means both Action and ChildAction instantiated its own instance of JobController.