Introduction:

In this article we will explain how to use Html.RenderAction() to return partial view from controller action in mvc or how to return and render partial view from controller action in mvc with example.

Description: 

Asp.Net MVC RenderAction method is use to displaying data in the partial view is independent from corresponding view model. For example:  In a blog to show tag or category list on each and every page, we would like to use RenderAction method since the list of category is populated by the different model.

  • Let’s see a demonstration example of Html.RenderAction() method 

Index.cshtml:

@{
    Layout = null;
}
 
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
 
        @{Html.RenderAction("CategoryList", "Home");}
 
    </div>
</body>
</html>


CategoryList.cshtml:

@model List<Tutorials.Category>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<div class="table-responsive" style="width:500px;">
    <table class="table">
        <thead>
            <tr>
                <th>#</th>
                <th>Category Name</th>
                <th>Created On</th>                
            </tr>
        </thead>
        <tbody>
            @foreach(var item in Model)
            {
            <tr>
                <td>@item.ID</td>
                <td>@item.CategoryNane</td>
                <td>@item.CreatedOn</td>               
            </tr>
            }
        </tbody>
    </table>
</div>

 

HomeController:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace Tutorials.Controllers
{
    public class HomeController : Controller
    {                public ActionResult Index()
        {
            return View();
        }
        public ActionResult CategoryList()
        {
            ArticleEntities db=new ArticleEntities ();
 
            return PartialView("CategoryList", db.Category.ToList());
        }
 
    }
}


Output:

 use RenderAction method in asp.net mvc to render partial view


I hope it will help to you.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like