Andrzej Siwek
Andrzej Siwek

Reputation: 300

Problem with Telerik Grid and MVC3

I got a little problem with MVC3 and Telerik Grid. I have the following model:

public class Country
{
    [Key]
    public int CountryID { get; set; }
    public string CountryName { get; set; }
    public string CountryShortName { get; set; } 

    //
    // Example 
    public ICollection<City> Citys { get; set; }
}

and my City model:

public class City
{
    [Key]
    public int CityID { get; set; }
    public string CityName { get; set; }

    //
    // .... 
    public virtual Country Country { get; set; }
}

And I use MVCScaffolding (..., using repositories) and I have:

public class CountryRepository : ICountryRepository
{
    ProjektContext context = new ProjektContext();

    public IQueryable<Country> All
    {
        get { return context.Country; }
    }

    public IQueryable<Country> AllIncluding(params Expression<Func<Country, object>>[] includeProperties)
    {
        IQueryable<Country> query = context.Country;
        foreach (var includeProperty in includeProperties) {
            query = query.Include(includeProperty);
        }
        return query;
    }
}
//... and more more more :)
public interface ICountryRepository
{
    IQueryable<Country> All { get; }
    IQueryable<Country> AllIncluding(params Expression<Func<Country, object>>[] includeProperties);
    // .. more more ....
}

and my controller and view:

public class CountryController : Controller
{
    private readonly ICountryRepository countryRepository;

    // If you are using Dependency Injection, you can delete the following constructor
    public CountryController() : this(new CountryRepository()) {}

    public CountryController(ICountryRepository countryRepository)
    {
        this.countryRepository = countryRepository;
    }

    //
    // GET: /Country/

    public ViewResult Index()
    {
        return View(countryRepository.AllIncluding(country => country.Citys));
    }

    [GridAction]
    public ActionResult _AjaxIndex()
    {
        return View(new GridModel<Country>
        {
            Data = countryRepository.AllIncluding(country => country.Citys)
        });
    }
}

My Index.cshtml:

@model IEnumerable<Test.Models.Country>
@{
    ViewBag.Title = "Index";
}
@(Html.TelCountryerik().Grid(Model)
    .DataBinding(dataBinding => dataBinding.Ajax().Select("_AjaxIndex", "Country"))
    .Name("Country")
    .Columns(columns =>
        {
            columns.Bound(c => c.CountryName);
            columns.Bound(c => c.CountryShortName);
        })
    .Sortable()
    .Pageable()
    .Groupable()
    .Filterable()
)
  1. I can't access to the City values from the Telerik Grid (master/detail grid). I try to follow an example and I don't get the correct result.
  2. The problem occurs when I try to sort the filter Grid (without detail of course). I have security errors but when I change _AjaxIndex in the code below everything is fine (of course without acces to Cities).

    [GridAction]
    public ActionResult _AjaxIndex()
    {
        return View(new GridModel<Country>
        {
            Data = countryRepository.All
        });
    }
    

Can anyone help me with my problem?

Upvotes: 0

Views: 1762

Answers (1)

Andrzej Siwek
Andrzej Siwek

Reputation: 300

I resolve problem on my own. Problem was behind bugs in Telerink Components which (if u download using NUGet) is not working correctly with jQuery 1.6+.

Here is link to the 'problem resolver :P' jQuery 1.6+ Telerink+MVC FIX

I have hope is Telerink will soon update wrong (old) version of components on the NUGet.

Here is example - tested

    @(Html.Telerik().ScriptRegistrar()
.jQuery(false)
.jQueryValidation(false)
.DefaultGroup(group => group
    .Add("~/Scripts/jquery-1.6.4.js")
    .Add("~/Scripts/jquery.validate.js")
    .Add("~/Scripts/modernizr-2.0.6-development-only.js")
    .Add("~/Scripts/2011.2.712/telerik.common.min.js")
    .Add("~/Scripts/2011.2.712/telerik.textbox.min.js")
    .Add("~/Scripts/2011.2.712/telerik.grid.min.js")
    .Add("~/Scripts/2011.2.712/telerik.draganddrop.min.js")
    .Add("~/Scripts/2011.2.712/telerik.grid.grouping.min.js")
    .Add("~/Scripts/2011.2.712/telerik.grid.filtering.min.js")
    .Add("~/Scripts/2011.2.712/telerik.grid.editing.min.js")
    .Add("~/Scripts/2011.2.712/telerik.window.min.js")
.Combined(true)
.Compress(true))
.OnDocumentReady(
@<text>
    prettyPrint();
</text>)
)

Upvotes: 1

Related Questions