2012-01-04 73 views
1

我有一個從多個部分視圖加載數據的頁面。部分觀點之一有幾個鏈接。說3.每次用戶點擊鏈接我重新加載頁面與點擊鏈接相關的數據。表格看起來像這樣。在MVC3中發佈表單

@model TestPostback.Models.HomeModel 
@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 

<form id="testform" method="post" action="@Url.Action("Index","Home")" runat="server"> 
    <div> 
     <table> 
      <tr> 
       <td>      
         <a href="@Url.Content("/Home/Index?selectedValue=Personal")"> This is a test page </a> 
       </td> 
      </tr>    
     </table> 
    </div> 
</form> 

<div> 
    <table> 
     <tr> 
      <td style="vertical-align:top;">           
       <h2 class="expand">Test Expand</h2> 
       <div class="collapse"> 
        <table id="testgrid" class="scroll" cellpadding="0" cellspacing="0"></table> 
       </div> 
      </td> 
     </tr> 
    </table> 
</div> 


<script type="text/javascript">  

    jQuery(document).ready(function() { 
     var grid = jQuery("#testgrid");   
     grid.jqGrid({ 
      url: 'Home/GridData/', 
      datatype: 'json', 
      mtype: 'POST', 
      colNames: ['Id', 'Votes', 'Title'], 
      colModel: [ 
         { name: 'Id', index: 'Id', width: 40, align: 'left' }, 
         { name: 'Votes', index: 'Votes', width: 40, align: 'left' }, 
         { name: 'Title', index: 'Title', width: 400, align: 'left'} 
        ], 
      rowNum: 10, 
      rowList: [5, 10, 20, 50], 
      sortname: 'Id', 
      sortorder: "desc", 
      viewrecords: true, 
      imgpath: '', 
      caption: 'My first grid' 
     });   
    });  
</script> 

控制器代碼: -

public ActionResult Index(string selectedValue) 
     { 
      return View(); 
     } 

     public JsonResult GridData(string sidx, string sord, int page, int rows) 
     { 
      int totalPages = 1; // we'll implement later 
      int pageSize = rows; 
      int totalRecords = 3; // implement later 

      var jsonData = new 
      { 
       total = totalPages, 
       page = page, 
       records = totalRecords, 
       rows = new[]{ 
        new {id = 1, cell = new[] {"1", "-7", "Is this a good question?"}}, 
        new {id = 2, cell = new[] {"2", "15", "Is this a blatant ripoff?"}}, 
        new {id = 3, cell = new[] {"3", "23", "Why is the sky blue?"}} 
       } 
      }; 
      return Json(jsonData); 
     } 

這裏就是我要做的。 當我點擊鏈接「這是一個測試頁面」時,它調用了一些值的控制器,但它不加載我的網格。我想要這個值,因爲我想根據用戶點擊加載具有不同值的網格。我必須做出什麼改變才能發揮作用?

請指教。

+0

你通過AJAX加載?這裏的代碼不顯示,既然你只有一個窗體,沒有顯示任何網格,我必須這樣想。更多詳情請:) – 2012-01-04 18:09:55

+0

我已經更新了這個問題。請指教。謝謝!!! – 2012-01-04 18:44:23

回答

2

我推薦以下the PRG (post redirect get) pattern:所以在發佈後,您將重定向到適合的任何操作方法(在本例中爲顯示所有jqGrid的方法)。使用MVC,可以實現using a RedirectResultController上可用的RedirectToAction方法。

+0

這不會工作,因爲我不得不從數據庫加載其他部分視圖與數據/文本。所以我想要做的是返回一個視圖,它加載我的部分視圖中的所有數據,然後調用加載我的網格數據的控制器方法。 – 2012-01-04 17:40:42