| by Achyut Kendre | 16 comments

What is Child Action Calls? How to in ASP.NET MVC 5?

ASP NET MVC 5

Example Source Code: https://drive.google.com/file/d/1dkgkEq9WR1b1hlDks-pEy8Fx6YBdEEgX/view?usp=sharing

What is Child Action Call?

When we call any action and that action returns view so that action execution will be completed when a view is returned but before completion of view processing if we call any action in between from the view that is called child action call whoes result can be loaded in a view before rendering the original view.

To make the child action call in ASP.NET MVC we have a helper called –

@Html.Action("ActionName",["ControllerName"],[RouteValue Parameters])

Let us understand it step by step how to create it –

Step 1: Create ASP.NET MVC web application as previously created.

Step 2 : Create a action with a name contact which returns a view as follows.

public class TestController : Controller
{
  public ActionResult Contact()
   {
      return View();
   }
}

Step 3: Also create two actions called SayHello() and SayBye() which returns string as follows –

public string SayHello()
{
 return "Say Hello method Called!";
}

public string SayBye()
{
  return "This is Say Bye Action Called!";
}

Step 4: Now call the SayHello and SayBye action using child action call from contact.cshtml view as follows –

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Contact</title>
</head>
<body>
    <div>  
        <h2> Contact view used to demo </h2>
        <p> this is contact view used to demo </p>

        <h2> Call Say Hello </h2>
        @Html.Action("SayHello")
        <h2>Call Say Bye </h2>
        @Html.Action("SayBye")
    </div>
</body>
</html>

You can also return a partial view from action using a method PartialView.

PartialView() : – returns partial view having name exactly similar to action name.
Partialview(model) :- returns a strongly typed partial view having same name as that of action name.
PartialView(“viewname”) : – returns a partial view having a name passed as parameter.
PartialView(“viename”,Model):- returns a strongly typed partial view having a name passed as parameter.

Now create three models to create three strongly typed partial view to be returned from three child action calls as follows –

public class Customer
    {
        public Int64 CustomerID { get; set; }
        public string CustomerName { get; set; }
        public string Address { get; set; }
        public string EmailID { get; set; }
        public decimal CreditLimit { get; set; }
    }
public class Emp
    {
        public Int64 EmpID { get; set; }
        public string EmpName { get; set; }
        public string DeptName { get; set; }
        public decimal Salary { get; set; }
    }
public class Product
    {
        public Int64 ProductID { get; set; }
        public string ProductName { get; set; }
        public string MfgName { get; set; }
        public decimal Price { get; set; }
    }

Now create three strongly typed partial view for every model as follows –

_Customre.cshtml

@model PartialViewExampleChildActionCalls.Models.Customer

<table border="1">
    <tr>
        <td>@Html.LabelFor(p=>p.CustomerID)</td>
        <td>@Html.DisplayFor(p=>p.CustomerID)</td>
    </tr>
    <tr>
        <td>@Html.LabelFor(p=>p.CustomerName)</td>
        <td>@Html.DisplayFor(p=>p.CustomerName)</td>
    </tr>
    <tr>
        <td>@Html.LabelFor(p=>p.Address)</td>
        <td>@Html.DisplayFor(p => p.Address)</td>
    </tr>
    <tr>
        <td>@Html.LabelFor(p=>p.EmailID)</td>
        <td>@Html.DisplayFor(p=>p.EmailID)</td>
    </tr>
    <tr>
        <td>@Html.LabelFor(p=>p.CreditLimit)</td>
        <td>@Html.DisplayFor(p=>p.CreditLimit)</td>
    </tr>
</table>

_Emp.cshtml

@model PartialViewExampleChildActionCalls.Models.Emp
<ol>
    <li>Emp ID:@Html.DisplayFor(p => p.EmpID)</li>
    <li>Emp ID:@Html.DisplayFor(p => p.EmpName)</li>
    <li>Emp ID:@Html.DisplayFor(p => p.DeptName)</li>
    <li>Emp ID:@Html.DisplayFor(p => p.Salary)</li>
</ol>

_Product.cshtml

@model PartialViewExampleChildActionCalls.Models.Product

<div style="width:400px;background-color:orange">
   Product ID: @Model.ProductID <br />
  Product Name: @Model.ProductName <br />
   Mfg Name:@Model.MfgName <br />
    Price :@Model.Price <br />
</div>

Create three child action actions those returns those partial views from child actions –

public ActionResult GetCust()
{
 Customer c = new Customer() {CustomerID=123,CustomerName="Akash",Address="Nigdi",CreditLimit=56000,EmailID="Akash@hotmail.com"};
            return PartialView("_Customer",c);
}
public ActionResult GetEmp()
{
  Emp e = new Emp() { EmpID = 1, EmpName = "Suresh", DeptName = "Computer", Salary = 56000 };
          return PartialView("_Emp", e);
}
public ActionResult GetProduct()
{
        Product p = new Product() {ProductID=121,ProductName="Mouse",Price=56000,MfgName="Intex"};
            return PartialView("_Product", p);
 }

No create the action to get main view where from we can make the child action calls to render these partial views as follows –

public ActionResult CombineData()
{
 return View();
}

Now create the CombineData View as follows –


@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>CombineData</title>
</head>
<body>
    <div> 
         <h2> Emp information </h2>
          @Html.Action("GetEmp")

        <hr />
        <h2> Customer Information </h2>
         @Html.Action("GetCust")
        <hr />
        <h2> Product information</h2>
        @Html.Action("GetProduct")

    </div>
</body>

</html>

16 Comments

web site

July 7, 2024 at 5:47 am Reply

My spouse and I absolutely love your blog and find many
of your post’s to be exactly what I’m looking for. can you offer guest writers to
write content in your case? I wouldn’t mind producing a post or elaborating on some of the subjects you write about here.
Again, awesome website!

Romeo Newmeyer

September 11, 2024 at 8:26 am Reply

Would you be interested in exchanging links?

Leland Ried

September 18, 2024 at 11:01 am Reply

Superb and well-thought-out content! If you need some information about SEO, then have a look at Article World

Warner Gifford

October 1, 2024 at 11:02 am Reply

Hey there, I love all the points you made on that topic. There is definitely a great deal to know about this subject, and with that said, feel free to visit my blog FQ6 to learn more about Airport Transfer.

Raina Cornejo

October 8, 2024 at 2:58 pm Reply

It is always great to come across a page where the admin take an actual effort to generate a really good article. Check out my website FQ5 concerning about Adsense.

Angel Weyer

October 13, 2024 at 10:46 pm Reply

This website was… how do you say it? Relevant!! Finally I’ve found something that helped me. Thanks.

카지노추천

October 16, 2024 at 4:35 am Reply

Way cool! Some extremely valid points! I appreciate you penning this write-up and the rest of the site is also very good.

blue dream

October 16, 2024 at 10:35 am Reply

Spot on with this write-up, I really think this web site needs a great deal more attention. I’ll probably be returning to read through more, thanks for the information!

Heraldberg

October 16, 2024 at 1:18 pm Reply

When I initially commented I appear to have clicked the -Notify me when new comments are added- checkbox and from now on each time a comment is added I receive four emails with the exact same comment. There has to be a means you are able to remove me from that service? Cheers.

Herald America News

October 16, 2024 at 3:53 pm Reply

Having read this I believed it was very enlightening. I appreciate you finding the time and energy to put this content together. I once again find myself personally spending a lot of time both reading and commenting. But so what, it was still worth it!

supplyhouse plumbing

October 16, 2024 at 6:27 pm Reply

Greetings, I do think your web site could be having web browser compatibility issues. When I take a look at your site in Safari, it looks fine however, when opening in I.E., it’s got some overlapping issues. I just wanted to give you a quick heads up! Aside from that, great site!

Dispensary Near Me

October 16, 2024 at 8:46 pm Reply

I want to to thank you for this excellent read!! I definitely loved every little bit of it. I have you book-marked to check out new things you post…

ücretsiz bonus veren siteler

October 17, 2024 at 12:38 am Reply

Deneme bonusu ile oyunlara sıfır yatırımla başlamak hiç bu kadar kazançlı olmamıştı!

daftar slot online

October 17, 2024 at 2:17 am Reply

I blog often and I truly thank you for your information. Your article has truly peaked my interest. I am going to take a note of your website and keep checking for new information about once a week. I subscribed to your Feed as well.

additional hints

October 17, 2024 at 7:52 am Reply

Casino’da bahis seçeneklerinin çeşitliliği sayesinde her oyunda farklı stratejiler deneyebiliyorum.

toptan poşet baskı

October 17, 2024 at 6:07 pm Reply

I was able to find good information from your articles.

Leave a Reply