Scott
Scott

Reputation: 2760

MVC Partial Page returns whole page, not just the partial

I have a button on a page:

                <div>
                    From:
                    <input id="fromDate" type="date" name="fromDate" value="@a.FromDate">
                    To:
                    <input id="toDate" type="date" value="@a.ToDate">
                    <button type="button" id="btn_transactions" class="btn btn-primary" onclick="btnTrans()"><span class="glyphicon glyphicon-search"></span></button>
                    <label class="buy-heading">Total Buys: @String.Format("{0:n0}", @q.BuyQuantity)</label><label class="sell-heading">Total Sells: @String.Format("{0:n0}", @q.SellQuantity)</label>
                </div>

It calls this function on click:

    function btnTrans() {

        var postdata = { "symbol": $("#savesymbol").val(), "fromDate": $("#fromDate").val(), toDate: $("#toDate").val() };
        var url = "Company/Transactions?symbol=" + $("#savesymbol").val() + "&fromDate=" + $("#fromDate").val() + "&toDate=" + $("#toDate").val();
        $.ajax({
            url: url,
            success: function (result) {
                alert(result)
                $('#transTarget').html(result);
            },
            error: function () {
                //alert("Error occured");
            }
        });
    }

The controller method is:

public async Task<PartialViewResult> Transactions(string symbol, string 
    fromDate, string toDate)
{

        return PartialView("Transactions");
}

This is the partial view:

<table id="transactionsTable" class="table table-bordered display hover no-wrap dataTables" style="width: 100%">
                <thead>
                    <tr>
                        <th>Account Category</th>
                        <th>Trade Date</th>
                        <th>Account Name</th>
                        <th>Activity</th>
                        <th>Quantity</th>
                        <th>Exec Price</th>
                        <th>Principal</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var item in Model)
                    {
                        var a = item.accounts;
                        foreach (var item2 in a)
                        {
                            <tr>
                                <td>@item2.AcctCategory</td>
                                <td>@String.Format("{0:MM/dd/yyyy}", @item2.TradeDate)</td>
                                <td>@item2.AccountName</td>
                                <td>@item2.Trans</td>
                                <td>@String.Format("{0:n}", item2.Quantity)</td>
                                <td>@String.Format("{0:n}", item2.ExecutionPrice)</td>
                                <td>@String.Format("{0:n}", item2.PrincipalAmount)</td>
                            </tr>
                        }
                    }
                </tbody>
            </table>

I have 3 partials on this page as follows:

            <div class="mycontent2">
            @{Html.RenderAction("Documents", "Company");}
        </div>
        <div class="mycontent2">
            @{Html.RenderAction("Holdings", "Company");}
        </div>
        <div class="mycontent2">
            @{Html.RenderAction("Transactions", "Company");}
        </div>

The problem is that the partial view returned from the controller method returns the WHOLE page, not just the partial part of transactions. What am I doing wrong?

Upvotes: 1

Views: 651

Answers (2)

Erman
Erman

Reputation: 192

Return a model from the controller. Like this;

return PartialView("Transactions", model);

Do it for others partial.

Then, you will use model in your view.

Upvotes: 2

Adriano Silva
Adriano Silva

Reputation: 2576

Use @{ Layout = null; } in your partial view.

Upvotes: 0

Related Questions