MVC Partial View Returns Whole Page Instead of Just the Partial

ajax asp.net-mvc asp.net-mvc-4 partial

1372 观看

1回复

852 作者的声誉

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?

作者: Scott 的来源 发布者: 2017 年 12 月 27 日

回应 1


4

44 作者的声誉

Add this code in partial view

@{
Layout=null;
}
作者: Uttam Suthar 发布者: 2017 年 12 月 29 日
32x32