2012-10-04 99 views
3

我有一個視圖,它包含一個包含通過ajax頁面的WebGrid的局部視圖。 但是,我無法使父視圖知道WebGrid所在的頁面。我試圖完成的是這樣的:我在包含View的導航到新頁面上的「創建」按鈕,但我希​​望它將當前頁面#作爲查詢字符串參數傳遞到新頁面,所以如果他們取消創建,原始視圖將加載前一頁,而不是默認加載第一頁。視圖如何知道ajax MVC WebGrid在哪個頁面上?

我看到的問題是,當部分視圖的網格更新時,父視圖的模型不會更改。而且,儘管我可以爲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

解決方案最終變得非常簡單,我有點尷尬多久才花了我的時間。

在_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; 
} 
相關問題