MVC Partial Page返回整个页面,而不仅仅是部分页面

时间:2018-01-29 19:23:41

标签: asp.net-mvc razor partial

我在页面上有一个按钮:

                <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>

点击后调用此功能:

    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");
            }
        });
    }

控制器方法是:

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

        return PartialView("Transactions");
}

这是局部视图:

<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>

我在此页面上有3个部分内容如下:

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

问题是从控制器方法返回的局部视图返回WHOLE页面,而不仅仅是事务的部分部分。我做错了什么?

2 个答案:

答案 0 :(得分:1)

从控制器返回一个模型。像这样;

return PartialView("Transactions", model);

为其他人做部分。

然后,您将在视图中使用模型。

答案 1 :(得分:0)

在局部视图中使用@{ Layout = null; }