我有一个页面,从多个部分视图加载数据。其中一个部分视图几乎没有链接。说3。每次用户单击链接时,我都会用与所单击链接相关的数据重新加载页面。表单是这样的:
@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);
}
这就是我要做的。当我点击链接"这是一个测试页面",它调用一些值的控制器,但它不加载我的网格。我想要这个值,因为我想加载基于用户点击不同值的网格。我需要做哪些改变才能让它起作用?
请建议。
我建议遵循PRG (post redirect get)模式:因此,在post之后,您将重定向到任何合适的操作方法(在本例中是显示所有jqGrids的操作方法)。对于MVC,这可以使用redirecresult或Controller
上可用的RedirectToAction
方法来实现。
你是否序列化你的数据当你得到它回来(即serializeGridData)
你是通过JSON加载吗?
如果是,请查看设置jQuery执行请求的内容类型jqGrid