asp.net-mvc-3partial-viewswebgrid

Paging/Sorting not working on web grid used in Partial View


I have a partial view where I am showing a web grid depending upon a value selected from a page.

For drop down I have used

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

For drop down item select I have used

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

and below is my action

public ActionResult Foo(string value)
 {
  SomeModel model = ...
return PartialView(model);
 }

everything works good, but when I try doing a paging or sorting on my webgrid which is on my partial view I am showing a new window with the web grid.

I wanted to be able to sort and page on the same page without postback

Please help


Solution

  • The following example works fine for me.

    Model:

    public class MyViewModel
    {
        public string Bar { get; set; }
    }
    

    Controller:

    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);
        }
    }
    

    Index.cshtml view:

    <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>
    

    Foo.cshtml partial:

    @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")
        )
    )
    

    Notice that I have used a GET request to refresh the grid instead of POST because this way the value query string parameter selected in the dropdown is preserved for future sorting and paging.