Introduction:

Here we will explain how to use Partial view in MVC or what is Partial view in MVC or how to reduce duplicate coding using Partial view or how to render partial view with passing parameter in MVC example.

Description:   

Asp.net MVC Html.Partial() helper method renders the partial view as HTML-encoded string. It takes two parameter and returns Html String.it 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 Html.Partial() method since the list of category is populated by the different model.

Let’s see a demonstration example of Html.Partial() helper method 

In this example we are passing model as parameter.

Index.cshtml:

@model List<Tutorials.Product>
    @{
        Layout = null;
    }
 
<!DOCTYPE HTML>
<html>
<head>
  <meta name="viewport" content="width=device-width" />
  <title>Reder Partial View</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"rel="stylesheet" />
        
    </head>
    <body>
        <div id="ProductList">
         @Html.Partial("ProductList", Model)
        </div>
    </body>
</html>


ProductList.cshtml:

@model List<Tutorials.Product>
 
<div class="table-responsive" style="width:500px;">
    <table class="table">
        <thead>
            <tr>
                <th>#</th>
                <th>Product Name</th>
                <th>Model No</th>
                <th>Rate</th>                
            </tr>
        </thead>
        <tbody>
            @foreach(var item in Model)
            {
            <tr>
                <td>@item.ID</td>
                <td>@item.ProductName</td>
                <td>@item.ModelNo</td>
                <td>@item.Rate</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()
        {
            ArticleEntities db = new ArticleEntities();
            return View(db.Product.ToList());
        }
    }
}

 

 

Output:

 how to render partial view with model parameter in asp.net mvc


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