Domanda

I'm currently stuck when trying to implement the sorting feature of the Kendo Grid control. When I click a column to sort the values, it takes me to a 404 page. None of the documentation I've looked at is binding the grid to a DataTable and I'm also wondering if there needs to be a specific action for the sorting. Can anyone help?

View Code:

    @{
        if (IsPost && Request.Url.AbsolutePath.Contains("Carriers"))
        {
                @(Html.Kendo().Grid((DataTable)(ViewData["CarrierResults"]))
                    .Name("carrierSearchResults")
                    .Sortable()
                )
        }
    }

Controller Code:

    [HttpPost]
    public ActionResult Carriers()
    {
        DealerPortalRepository dpr = new DealerPortalRepository();
        // The SearchForCarriers method below returns a DataTable
        ViewData["CarrierResults"] = dpr.SearchForCarriers();  
        return View("~/Views/Search/Index.cshtml");
    }

Edit: Query code:

    public DataTable SearchForCarriers()
    {
        var query = from a in db.Affiliates
                    where a.AffiliateLevel == 1
                    select new { a.AffiliateName };
        return createCarrierTable(query);
    }

    public DataTable createCarrierTable(IEnumerable<dynamic> query)
    {
        // Create new DataTable since the query above does not produce a DataRow that follows a schema already defined in the database.
        DataTable dt = new DataTable();
        dt.Columns.Add(
            new DataColumn()
            {
                DataType = System.Type.GetType("System.String"),
                ColumnName = "Affiliate Name"
            }
        );

        // Add the row(s) to the DataTable.
        foreach (dynamic item in query)
        {
            var row = dt.NewRow();
            row["Affiliate Name"] = item.AffiliateName;
            dt.Rows.Add(row);
        }

        return dt;
    }

P.S. I know I should probably be using a model instead of ViewData, but I didn't originally implement it that way because the anonymous type in my DataTable was throwing me off.

È stato utile?

Soluzione

Since I cannot see the rest of your code, here is my guess.

Can you remove [HttpPost] and test it again?

If still doesn't work, you need another action method which accepts DataSourceRequest as parameter.

public ActionResult Carriers_Read([DataSourceRequest]DataSourceRequest request)
{
  ...
}

Look at Grid Ajax Binding.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top