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()
)
- I can't access to the
Cityvalues from the Telerik Grid (master/detail grid). I try to follow an example and I don't get the correct result. The problem occurs when I try to sort the filter Grid (without detail of course). I have security errors but when I change
_AjaxIndexin 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?