2011-09-13 61 views
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() 
) 
  1. 我无法访问从Telerik的网格(主/从网格)的City值。我试图按照一个例子,我没有得到正确的结果。
  2. 当我尝试对过滤网格进行排序时(当然没有详细说明),就会出现问题。我有安全错误,但是当我在下面的代码中更改_AjaxIndex时,一切都很好(当然没有访问城市)。

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

谁能帮助我与我的问题?

回答

1

我自己解决问题。问题出在Telerink组件后面的错误(如果你使用NUGet下载)不能正常使用jQuery 1.6+。

这里是链接到“问题解析:P” jQuery 1.6+ Telerink+MVC FIX

我希望是Telerink即将更新上的NuGet组件的错误(旧)版本。

下面是示例 - 测试

@(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>) 
)