Telerik Grid和MVC3的问题

时间:2011-09-13 10:36:44

标签: c# asp.net-mvc-3 telerik telerik-grid telerik-mvc

我对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)
        });
    }
}

我的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 Grid(主/详细网格)中的City值。我试着效仿一个例子而且得不到正确的结果。
  2. 当我尝试对过滤器网格进行排序时,会出现问题(当然没有详细信息)。我有安全错误,但是当我在下面的代码中更改_AjaxIndex时,一切都很好(当然没有访问城市)。

    [GridAction]
    public ActionResult _AjaxIndex()
    {
        return View(new GridModel<Country>
        {
            Data = countryRepository.All
        });
    }
    
  3. 任何人都可以帮我解决问题吗?

1 个答案:

答案 0 :(得分: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>)
)