2016-03-09 76 views
1

我在我的mvc應用程序中使用WebGrid。如何刷新mvc webgrid

<div class="grid-div" id="webgridid"> 
       @grid.GetHtml(
    tableStyle: "gridTable", 
    headerStyle: "gridHead", 
    footerStyle: "gridFooter", 
    columns: new[] 
    { 
     grid.Column("name","Name", canSort: true, style: "name"), 
     grid.Column("description","Description", canSort: true, style: "description"), 
     grid.Column("duration","Duration", canSort: true, style: "duration"), 
    }) 
</div> 

我可以使用表格編輯選定的行值。編輯這個值後,它不反映在我的webGrid中。但它反映在DataBase中。爲了將其反映到webGrid中,我需要通過從數據庫加載數據來刷新webGrid。如何從數據庫重新加載內容到webGrid?在重新加載之後,這個pageNumber應該是舊的。這個怎麼做?

+0

你有沒有看過使用部分和調用與jQuery jQuery的動作。 [示例這裏](http://stackoverflow.com/questions/1570127/render-partial-view-using-jquery-in-asp-net-mvc) –

回答

3

最後我找到解決我的問題。我必須定義一個<div>ID,並參考的idajaxUpdateContainerId屬性的WebGrid控件,這將允許WebGrid使用Ajax異步更新數據。

<div id="ajaxgrid"> 
    @{ 
     var grid = new WebGrid(Model, rowsPerPage: 10, selectionFieldName: "SelectedRow", ajaxUpdateContainerId: "ajaxgrid");     
    } 
</div> 

然後,調用的WebGridGetHtml方法,使得剃刀Engine可以生成對應HTML它。標記<div>結束後。

@grid.GetHtml(
    tableStyle: "gridTable", 
    headerStyle: "gridHead", 
    footerStyle: "gridFooter", 
    columns: new[] 
    { 
     grid.Column("name","Name", canSort: true, style: "name"), 
     grid.Column("description","Description", canSort: true, style: "description"), 
     grid.Column("duration","Duration", canSort: true, style: "duration"), 
    }) 

然後在我ajax呼籲更新用我加入location.reload()刷新我的WebGrid

$.ajax({ 
       url: '@Url.Action("User", "UserDetails")', 
       type: "POST", 
       dataType: "json", 
       contentType: "application/json; charset=utf-8", 
       data: formData, 
       contentType: false, 
       processData: false, 
       success: function (result) { 
        if (result == "OK") { 
         location.reload(); 
        } 
        else 
         alert(result); 
       }, 
       error: function (result) { 
        alert('Error!'); 
       } 
      }); 

現在它對我來說工作得很好。

+1

感謝您的! location.reload();就是我需要的工作,以滿足我需要的工作! – AxleWack