分页/排序不适用于部分视图中使用的网格

Yas*_*ser 3 partial-views webgrid asp.net-mvc-3

我有局部视图,我根据从页面中选择的值显示网格.

对于下拉我用过

 @Html.DropDownListFor(
x => x.ItemId,
new SelectList(Model.Items, "Value", "Text"),
new { 
    id = "myddl", 
    data_url = Url.Action("Foo", "SomeController")
}
)
Run Code Online (Sandbox Code Playgroud)

对于下拉项目选择我使用过

$(function() {
$('#myddl').change(function() {
   var url = $(this).data('url');
   var value = $(this).val();
   $('#result').load(url, { value: value })
});
});
Run Code Online (Sandbox Code Playgroud)

以下是我的行动

public ActionResult Foo(string value)
 {
  SomeModel model = ...
return PartialView(model);
 }
Run Code Online (Sandbox Code Playgroud)

一切都很好,但当我尝试在我的部分视图上的webgrid上进行分页或排序时,我正在显示一个带有网格的新窗口.

我希望能够在没有回发的情况下对同一页面进行排序和分页

请帮忙

Dar*_*rov 8

以下示例适用于我.

模型:

public class MyViewModel
{
    public string Bar { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Foo(string value)
    {
        var model = Enumerable.Range(1, 45).Select(x => new MyViewModel
        {
            Bar = "bar " + value + " " + x
        });
        return PartialView(model);
    }
}
Run Code Online (Sandbox Code Playgroud)

Index.cshtml 视图:

<script type="text/javascript">
    $(function () {
        $('#myddl').change(function () {
            var url = $(this).data('url');
            var value = $(this).val();
            $.ajax({
                url: url,
                type: 'GET',
                cache: false,
                data: { value: value },
                success: function (result) {
                    $('#result').html(result);
                }
            });
        });
    });
</script>

@Html.DropDownList(
    "id",
    new[] {
        new SelectListItem { Value = "val1", Text = "value 1" },
        new SelectListItem { Value = "val2", Text = "value 2" },
        new SelectListItem { Value = "val3", Text = "value 3" },
    },
    new {
        id = "myddl",
        data_url = Url.Action("Foo", "Home")
    }
)

<div id="result">
    @Html.Action("Foo")
</div>
Run Code Online (Sandbox Code Playgroud)

Foo.cshtml 部分:

@model IEnumerable<MyViewModel>
@{
    var grid = new WebGrid(
        canPage: true, 
        rowsPerPage: 10, 
        canSort: true, 
        ajaxUpdateContainerId: "grid"
    );
    grid.Bind(Model, rowCount: Model.Count());
    grid.Pager(WebGridPagerModes.All);
}

@grid.GetHtml(
    htmlAttributes: new { id = "grid" },
    columns: grid.Columns(
        grid.Column("Bar")
    )
)
Run Code Online (Sandbox Code Playgroud)

请注意,我使用了GET请求来刷新网格而不是POST,因为这样value在下拉列表中选择的查询字符串参数将被保留,以便将来进行排序和分页.