Telerik Grid 和 MVC3 的问题

发布于 2024-12-04 05:22:38 字数 3412 浏览 5 评论 0原文

我在使用 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()
)
  1. 我无法从 Telerik 网格(主/详细网格)访问 City 值。我尝试遵循一个例子,但没有得到正确的结果。
  2. 当我尝试对过滤器网格进行排序时出现问题(当然没有详细信息)。我有安全错误,但是当我更改下面代码中的 _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()
)
  1. 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.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少心凉 2024-12-11 05:22:38

我自己解决问题。问题在于 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>)
)

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

    @(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>)
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文