Scott
Scott

Reputation: 2760

MVC Partial View Returns Whole Page Instead of Just the Partial

I have the following partial view named "_transactions":

<div id="section-transactions" class="documentsanchor">
</div>
<div>
    <div class="row">
        <div class="col-lg-12">
            <div>
                <h4 class="company-headings">@ViewBag.SecSymbol Transactions</h4>
            </div>
        <div>
    </div>
</div>

I render it using

 @{Html.RenderAction("Transactions", "Company");}

and this is the Transactions method from the Company controller:

    public async Task<PartialViewResult> Transactions()
    {
        ViewBag.SecSymbol = "test";

        return PartialView("_transactions");
    }

It's on a page with other partial views.

This works fine. However, I have a button on the page that should get a new partial view and replace the current one. It makes an ajax call as follows

    $("#btn_transactions").click(function (e) {           
        var url = "Company/Transactions";
        $.ajax({
            url: url,
            success: function (result) {
                alert(result);
                $('#transTarget').html(result);
            },
            error: function () {
                alert("Error occured");
            }
        });
    })

The issue is that the whole page is returned in "result", that is, all partials as well as the layout, when all I want is the transactions partial. What am I doing wrong?

Upvotes: 5

Views: 3759

Answers (2)

seanjacob
seanjacob

Reputation: 2258

Make sure you have the route set up in your configs -

routes.MapRoute("CompanyTransactions", "company/transactions", new { controller = "Company", action = "Transactions" });

Upvotes: 0

Uttam Suthar
Uttam Suthar

Reputation: 106

Add this code in partial view

@{
Layout=null;
}

Upvotes: 7

Related Questions