Telerik Grid 和 MVC3 的问题
我在使用 MVC3 和 Telerik Grid 时遇到了一些问题。我有以下模型:
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)
});
}
}
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()
)
- 我无法从 Telerik 网格(主/详细网格)访问
City
值。我尝试遵循一个例子,但没有得到正确的结果。 当我尝试对过滤器网格进行排序时出现问题(当然没有详细信息)。我有安全错误,但是当我更改下面代码中的
_AjaxIndex
时,一切都很好(当然无法访问城市)。[GridAction] 公共 ActionResult _AjaxIndex() { return View(new GridModel<国家>; { 数据=countryRepository.All }); }
谁能帮我解决我的问题吗?
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
City
values 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
_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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己解决问题。问题在于 Telerink 组件中的错误,该组件(如果您使用 NUGet 下载)无法与 jQuery 1.6+ 正常工作。
这是“问题解决程序:P”的链接
jQuery 1.6+ Telerink+ MVC FIX
我希望 Telerink 能够很快更新 NUGet 上错误(旧)版本的组件。
这是示例 - 经过测试
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