Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a IndexPartial which is a Partial View and it contains a table which should show a simiple Table from Customer Model Class. I also have another Main page which should Post the IndexPartial View into the Main View on the button click action but it isnt.

What I have tried:

My Main.cshtml code

<pre>@model PartialViews.Models.Customer

@{
    ViewBag.Title = "Main";
}

<body>

    <input type="button" value="Click" id="btnClick" />
    <div class="container">
        <h2>Customer's List</h2>
        <div id="dvCustomerDetails" style="width: 50%; height: 130px; display: none">
        </div>


        @*@{
            Html.RenderAction("Index", "Customers");
            <div>@Html.Partial("IndexPartial")</div>}*@
    </div>

    <div id="output">

    </div>


</body>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>


<script type="text/javascript">
    $(document).ready(function () {
        $('#btnClick').click(function () {

            $.ajax({
                url: '/Customers/IndexPartial',
                dataType: 'html',
                contentType: 'application/html',
                type: 'Post',
                success: function (data) {
                    $('#dvCustomerDetails').html(data);
                }
            });

            if (document.getElementById("tableCount")) {

                var rowCount = $('#tableCount tr').length;
                var totalRow = rowCount - 1;
                document.getElementById('output').innerHTML = totalRow;
            } else {
                alert("Table doesn't exists yet");
            }
        });
    });
</script>


My CustomersController's IndexPartial Action

[HttpPost]
public ActionResult IndexPartial()
{
    AdventureWorksLT2012Entities adv = new AdventureWorksLT2012Entities();

    return PartialView("IndexPartial", adv.Customers.ToList());

}



My IndexPartial.cshtml

@using System.Runtime.InteropServices.ComTypes
@using PartialViews.Models
@model IList<PartialViews.Models.Customer>

<table class="table table-responsive">
    <tr>
        <td>
            Title
        </td>
        <td>
            Firstname
        </td>
        <td>
            Lastname
        </td>
        <td>
            Email
        </td>
        <td>
            Phone
        </td>
        <td>
            Date
        </td>
    </tr>
    
    @foreach (var item in Model)
    {
        <tr>
            @item.Title
        </tr><tr>
            @item.FirstName
        </tr><tr>
            @item.LastName
        </tr><tr>
            @item.EmailAddress
        </tr><tr>
            @item.ModifiedDate
        </tr>
    }
</table>
Posted
Updated 20-Dec-18 0:38am

1 solution

Html.Partial simply injects the result of a partial view (cshtml file) into that location, it doesn't call an action on a controller. If you need it to call your controller use Html.Action

https://www.c-sharpcorner.com/article/html-action-and-html-renderaction-in-Asp-Net-mvc/[^]
 
Share this answer
 
Comments
MaddoxSt 20-Dec-18 22:32pm    
I have done with that but i want to use ajax to call that view so later on my Main page i can have like 10 partial views which will not affect the loading of the Main page as my main goal is to load all the partial views separately but inside one page using Ajax so each partial views can load at their own times but in the same page
Graeme_Grant 21-Dec-18 2:55am    
Debug so you can actually see what is created. Exporting JSON is a different method call.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900