2012-01-04 54 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