This is a migrated thread and some comments may be shown as answers.

[Solved] Sort Clears All Populated Rows

0 Answers 18 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Dave
Top achievements
Rank 1
Dave asked on 25 Apr 2012, 02:36 AM
Hi there,

I'm currently evaluating the Telerik MVC grid for use on a corporate intranet project. I'll be populating the grid through stored procedures, and I want to take advantage of the sorting/filtering/etc capabilities in the Grid. I've built a sample project to populate the grid; it works fine until I try to sort the grid - when I do this, the entire control clears and the populated rows disappear. This is because my Index controller method is getting re-called with an empty strongly-typed Model object. Ideally, I was hoping there's be no HTTP call at all, and the sorting could occur client-side, but at this point I'd also be happy with a server-side solution. 

In another Telerik grid thread ( http://www.telerik.com/community/forums/aspnet-mvc/grid/grid-sort-column.aspx ) Atnas Korchev advocates passing the search query back and forth using the ViewData object. I'm not keen on this for two reasons:

- My search "query" is likely to be multiple fields - all associated with the strongly-typed Model object. I don't want to pass N fields as separate entities within ViewData.
- I don't actually want to use ViewData at all - I would like to rely exclusively on the strongly-typed Model object.

I've included my code below, and also an attached copy of my project. Any thoughts would be much appreciated.

// View code
@using Telerik.Web.Mvc.UI
@model SortYourselfOut.Models.FooModel
@{
    ViewBag.Title = "Home Page";
}


<h2>@ViewBag.Message</h2>


@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
     @Html.TextBox("Title", Model.Title)
     <button class="t-button" type="submit">Select</button>
}


@{
    Html.Telerik().Grid(Model.Items)
                         .Name("MyGrid")
                         .Sortable()
                         .Filterable()
                         .Groupable()
                         .Columns(
                            columns =>
                                {
                                    columns.Bound(o => o.Name).Title("Name").Width(200);
                                    columns.Bound(o => o.Number).Title("Number").Width(200);
                                }
                         )
                         .Render();
}




// Controller code
 public class HomeController : Controller
    {
        public ActionResult Index(FooModel model)
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";
            if (model.Title == null)
            {
                model = new FooModel();
                model.Title = "foobar";
            }
            else
            {
                // Populate the model with some stuff
                ModelState.Clear();
                model.Title = model.Title.ToUpper();
                List<Item> l = new List<Item>();
                for (int i = 0; i < 10; i++)
                {
                    Item it = new Item();
                    it.Name = model.Title + i.ToString();
                    it.Number = i;
                    l.Add(it);
                }
                model.Items = l;
            }
            return View(model);
        }
    }




// Model code
public class Item
    {
        public string Name { get; set; }
        public int Number { get; set; }
    }
    public class FooModel
    {
        public string Title { get; set; }
        public IEnumerable<Item> Items { get; set; }
    }






Tags
Grid
Asked by
Dave
Top achievements
Rank 1
Share this question
or