在MVC3中发布表单

时间:2012-01-04 17:22:00

标签: asp.net-mvc-3

我有一个从多个部分视图加载数据的页面。部分视图之一的链接很少。说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);
        }

这是我想要做的。 当我点击链接“这是一个测试页面”时,它会调用控制器一些值,但它不会加载我的网格。我想要这个值,因为我想根据用户点击加载具有不同值的网格。我必须做些什么改变呢?

请告知。

2 个答案:

答案 0 :(得分:2)

我建议关注the PRG (post redirect get) pattern:因此,在帖子之后,您可以重定向到适合的任何操作方法(在这种情况下是显示所有jqGrids的方法)。使用MVC,可以在RedirectToAction上实现using a RedirectResultController方法。

答案 1 :(得分:0)

您是否在获取数据时序列化数据(即serializeGridData)

你是用JSON加载的吗?

如果是,请退房 Setting the content-type of requests performed by jQuery jqGrid