0
我遇到了MVC3和Telerik Grid的一些问题。我有以下型号:Telerik Grid和MVC3的问题
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; }
}
和我City
型号:
public class City
{
[Key]
public int CityID { get; set; }
public string CityName { get; set; }
//
// ....
public virtual Country Country { get; set; }
}
而且我用MVCScaffolding
(...,使用资料库),我有:
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 ....
}
和我的控制器和视图:
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)
});
}
}
我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()
)
- 我无法访问从Telerik的网格(主/从网格)的
City
值。我试图按照一个例子,我没有得到正确的结果。 当我尝试对过滤网格进行排序时(当然没有详细说明),就会出现问题。我有安全错误,但是当我在下面的代码中更改
_AjaxIndex
时,一切都很好(当然没有访问城市)。[GridAction] public ActionResult _AjaxIndex() { return View(new GridModel<Country> { Data = countryRepository.All }); }
谁能帮助我与我的问题?