Javascript 存在ajax调用时WebGrid分页不工作
我试图通过ajax调用实现简单网格。当我在索引页面上单击submit时,一个网格应该显示为一个通过ajax代码发送的键。但当我转到第2页时,当代码再次转到控制器时,键变为null,并且网格不显示。 索引页:Javascript 存在ajax调用时WebGrid分页不工作,javascript,ajax,asp.net-mvc,webgrid,Javascript,Ajax,Asp.net Mvc,Webgrid,我试图通过ajax调用实现简单网格。当我在索引页面上单击submit时,一个网格应该显示为一个通过ajax代码发送的键。但当我转到第2页时,当代码再次转到控制器时,键变为null,并且网格不显示。 索引页: @{ ViewBag.Title = "index"; } <h2>index</h2> <button id="btn" onclick="divFunction()" value="submit">Submit</button>
@{
ViewBag.Title = "index";
}
<h2>index</h2>
<button id="btn" onclick="divFunction()" value="submit">Submit</button>
<div id="gridcontainer">
</div>
<script type="text/javascript">
function divFunction() {
results();
};
function results() {
$("#gridcontainer").show();
$.ajax(
{
type: 'Post',
url: "@Url.Action("List","User")",
data: { key: "hello" },
begin: function () {
$("#gridcontainer").hide();
},
success: function (data) {
$("#gridcontainer").show();
$("#gridcontainer").html(data);
}
});
}
</script>
@{
ViewBag.Title=“index”;
}
指数
提交
函数divFunction(){
结果();
};
函数结果(){
$(“#gridcontainer”).show();
$.ajax(
{
键入:“Post”,
url:“@url.Action(“列表”、“用户”)”,
数据:{key:“hello”},
开始:函数(){
$(“#gridcontainer”).hide();
},
成功:功能(数据){
$(“#gridcontainer”).show();
$(“#gridcontainer”).html(数据);
}
});
}
控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel;
using System.Web.UI.HtmlControls;
using WebApplication1.Models;
namespace MVCSimpleWebgrid.Controllers
{
public class UserController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult List(string key)
{
List<NumberModels> num = new List<NumberModels>();
if (key == "hello")
{
//List<NumberModels> num = new List<NumberModels>();
for (int n = 0; n < 10; n++)
{
NumberModels nu = new NumberModels();
nu.index = n;
num.Add(nu);
}
return View(num);
}
else
{
return null;
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用系统组件模型;
使用System.Web.UI.HTMLControl;
使用WebApplication1.模型;
命名空间MVCSimpleWebgrid.Controllers
{
公共类UserController:Controller
{
公共行动结果索引()
{
返回视图();
}
公共操作结果列表(字符串键)
{
List num=新列表();
如果(键==“你好”)
{
//List num=新列表();
对于(int n=0;n<10;n++)
{
NumberModels nu=新的NumberModels();
nu.index=n;
num.Add(nu);
}
返回视图(num);
}
其他的
{
返回null;
}
}
}
}
WebGrid代码:
@model IEnumerable<WebApplication1.Models.NumberModels>
@{
ViewBag.Title = "List";
var grid = new WebGrid(source: Model, canPage: true, rowsPerPage: 5);
grid.Pager(WebGridPagerModes.All);
}
<style type="text/css">
/*Here we will add css for style webgrid*/
.webgrid-table {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
font-size: 1.2em;
width: 100%;
display: table;
border-collapse: separate;
border: solid 1px #98BF21;
background-color: white;
}
.webgrid-table td, th {
border: 1px solid #98BF21;
padding: 3px 7px 2px;
}
.webgrid-header {
background-color: #A7C942;
color: #FFFFFF;
padding-bottom: 4px;
padding-top: 5px;
text-align: left;
}
.webgrid-footer {
}
.webgrid-row-style {
padding: 3px 7px 2px;
}
.webgrid-alternating-row {
background-color: #EAF2D3;
padding: 3px 7px 2px;
}
</style>
<div id="content">
@grid.GetHtml(
tableStyle: "webgrid-table",
headerStyle: "webgrid-header",
footerStyle: "webgrid-footer",
alternatingRowStyle: "webgrid-alternating-row",
rowStyle: "webgrid-row-style",
columns: grid.Columns(
grid.Column(columnName:"index",header:"serial")
))
</div>
@model IEnumerable
@{
ViewBag.Title=“列表”;
var grid=newwebgrid(来源:Model,canPage:true,rowsPerPage:5);
Pager(WebGridPagerModes.All);
}
/*这里我们将为样式webgrid添加css*/
.webgrid表{
字体系列:“投石机MS”,Arial,Helvetica,无衬线;
字体大小:1.2米;
宽度:100%;
显示:表格;
边界塌陷:分离;
边框:实心1px#98BF21;
背景色:白色;
}
.webgrid表td,th{
边框:1px实心#98BF21;
填充:3px 7px 2px;
}
.webgrid标题{
背景色:#A7C942;
颜色:#FFFFFF;
垫底:4px;
垫面:5px;
文本对齐:左对齐;
}
.webgrid页脚{
}
.webgrid行样式{
填充:3px 7px 2px;
}
.webgrid交替行{
背景色:#EAF2D3;
填充:3px 7px 2px;
}
@grid.GetHtml(
表样式:“webgrid表”,
标题样式:“webgrid标题”,
页脚样式:“webgrid页脚”,
交替行样式:“webgrid交替行”,
行样式:“webgrid行样式”,
列:grid.columns(
grid.Column(columnName:“index”,header:“serial”)
))
在ajax加载使用中。。。。。
GET请求刷新网格,而不是POST