View如何知道ajax MVC WebGrid的页面是什么?

时间:2012-10-04 20:58:15

标签: asp.net-mvc-3 webgrid

我有一个视图,其中包含一个包含通过ajax页面的WebGrid的部分视图。 但是,我无法让父视图知道WebGrid所在的页面。我想要完成的是:我在包含View的导航到新页面的“创建”按钮,但我希​​望它将当前页面#作为查询字符串参数传递给新页面,因此,如果他们取消创建,原始视图将加载前一页而不是默认加载第一页。

我看到的问题是,当局部视图的网格更新时,父视图的模型不会改变。而且,虽然我可以为ajaxUpdateCallback创建一个方法,但我还没弄明白如何告诉这个方法当前页面是什么。

我的代码的相关部分如下所示: 在父视图上:

<div id="divList">
    @Html.Partial("_MatterList", Model) //This is where the grid is defined
</div>
<br/>
<div id="newSection">

<input type="button" value="New Matter" onclick="newMatter();"></input>
</div>
<script type="text/javascript">

    var _Page; //I tried creating a javascript variable to hold the page, but can't get it to populate with anything but the default 1.

    function newMatter() 
    {
        var urlAction = "@Url.Action("Create", "Matter", new { area = "Admin" })";
        var subclientid = $("#SubClientID > option:selected").attr("value"); //defined in a form not displayed here
        var clientid = $("#ClientID > option:selected").attr("value");//defined in a form not displayed here
        redirect(urlAction, clientid, subclientid);

    }
    function redirect(urlAction, clientid, subclientid) {
        if(clientid > 0 || subclientid > 0 ) {
            urlAction += "?";
        }
        if(clientid > 0) {
            urlAction += "clientid=" + clientid;
            if(subclientid > 0) {
                urlAction += "&subclientid=" +subclientid;
            }
        }
        //I want to be able to add page in here as well. a la &page=_Page
        window.location = urlAction;
    }

在_MatterList局部视图中:

 <div id="matterListing">

    @{

        var grid = new WebGrid<Matter>(null, rowsPerPage: 10, canSort: false, defaultSort: "Name", ajaxUpdateContainerId: "matterListing",ajaxUpdateCallback:"afterUpdate");
        grid.Bind(Model.Matters, rowCount: Model.TotalRows, autoSortAndPage: false);
        @grid.GetHtml(
            tableStyle: "table90",
            alternatingRowStyle: "tableAltRows",
            headerStyle: "tableHeaders",
            columns: grid.Columns(

                grid.Column(...  //there are a lot of columns, removed them because not important to the problem.


                )
                   )
    }
    <script type="text/javascript">
        function afterUpdate() {
            _Page = @Model.PageNumber; //I tried this but it only ever shows 1 because it evaluates when the page first loads.  What I need is for it to dynamically get the correct page at execution time.
        }
    </script>
</div>

我在想的是,afterUpdate方法需要在当前页面中获取参数(即调用afterUpdate(2)而不是使用@ Model.PageNumber),但我不知道如何强制WebGrid在回调上传递它。有没有人有解决这个问题的方法?

1 个答案:

答案 0 :(得分:1)

解决方案最终变得非常简单,我有点尴尬,我花了多长时间来提出它。

在_MatterList局部视图中,我创建了表单并放置了一个带有页码的HiddenField:

<form id="frmPage">
    @HiddenFor(model => model.PageNumber)
</form>

然后在主页面中我修改了newMatter()以提取该页面,并重定向()将其添加为查询字符串参数:

function newMatter() 
{
    var urlAction = "@Url.Action("Create", "Matter", new { area = "Admin" })";
    var subclientid = $("#SubClientID > option:selected").attr("value"); //defined in a form not displayed here
    var clientid = $("#ClientID > option:selected").attr("value");//defined in a form not displayed here
    var page = $("#frmPage").find("#PageNumber").val();
    redirect(urlAction, clientid, subclientid, page);
}

function redirect(urlAction, clientid, subclientid, page)
{
        if(clientid > 0 || subclientid > 0  || page > 1) {
            urlAction += "?";
        }
        if(clientid > 0) {
            urlAction += "clientid=" + clientid;
            if(subclientid > 0) {
                urlAction += "&subclientid=" +subclientid;
            }
            if(page > 1) {
                urlAction += "&page=" + page;
            }
        }
        else if(page > 0){
            urlAction += "page=" + page;
        }
        window.location = urlAction;
}