我已经使用Kendo Grid显示数据。似乎数据正确传递,因为当我在运行时跟踪代码时,我看到有一些数据在结果中但是Kendo Grid无法显示数据。 我该如何解决这个问题?
编辑说明:
我找到了问题原因,但我无法解决。 如果我在QueryBuilder()方法中的ViewModel中删除此行,
Tags = article.ArticleTags.Where(c => c.ArticleId == article.Id).Select(b => b.Tag).Distinct()。ToList()
网格显示数据,但我需要标记的值。为什么这行代码导致了问题?
标记模型:
public class Tag : Entity, ITag
{
public Tag()
{
}
public virtual string Title { get; set; }
public virtual string Description { get; set; }
public virtual bool? IsActive { get; set; }
[Range(1, 4)]
public virtual int Size { get; set; }
public virtual ISet<ArticleTag> ArticleTags { get; set; }
public virtual ISet<ProjectTag> ProjectTags { get; set; }
}
我的网格:
@using Jahan.Blog.Web.Mvc.HtmlHelpers
@using Kendo.Mvc.UI
@using Kendo.Mvc.UI.Fluent
@model IEnumerable<Jahan.Blog.ViewModel.ArticleViewModel>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<div style="width: 100%;">
@(Html.Kendo().Grid<Jahan.Blog.ViewModel.ArticleViewModel>()
.Name("ArticleAdmin").Navigatable()
.Resizable(c => c.Columns(true))
.HtmlAttributes(new { @class = "cursorLink", @style = "width: 1000px;height:auto;overflow: scroll;" })
.Columns(columns =>
{
columns.Bound(p => p.Id).Width(100);
columns.Bound(p => p.Title).Width(200);
columns.Command(command => command.Destroy()).Width(170);
})
.ToolBar(toolbar =>
{
toolbar.Create();
toolbar.Save();
})
.Editable(editable => editable.Mode(GridEditMode.InCell))
.Pageable()
.Navigatable()
.Sortable()
.Scrollable()
.DataSource(dataSource => dataSource
.Ajax()
.Batch(true)
.PageSize(10)
.ServerOperation(false)
.Events(events => events.Error("error_handler"))
.Model(model => model.Id(p => p.Id))
.Create("Editing_Create", "ArticleAdmin")
.Read("Editing_Read", "ArticleAdmin")
.Update("Editing_Update", "ArticleAdmin")
.Destroy("Editing_Destroy", "ArticleAdmin")
))
</div>
在我的控制器中:
public ActionResult Index([DataSourceRequest] DataSourceRequest request)
{
List<ArticleViewModel> instance = new ArticleViewModel().FindByCriteria().ToList();
return View(instance); // There are some data. instance.count = 2
}
public ActionResult Editing_Read([DataSourceRequest] DataSourceRequest request)
{
List<ArticleViewModel> instance = new ArticleViewModel().FindByCriteria().ToList();
DataSourceResult dsRequest = instance.ToDataSourceResult(request); // There are some data.
return Json(dsRequest, JsonRequestBehavior.AllowGet);
}
我的ViewModel:
public class ArticleViewModel : IArticle, IDateTracking
{
public ArticleViewModel()
{
}
public int? UserId { get; set; }
public string Title { get; set; }
public string Summary { get; set; }
public string Description { get; set; }
public decimal? RateCounter { get; set; }
public int? LikeCounter { get; set; }
public bool IsActive { get; set; }
public bool IsActiveNewComment { get; set; }
public IList<Comment> Comments { get; set; }
public ISet<Rating> Ratings { get; set; }
public IList<AttachmentFile> AttachmentFiles { get; set; }
public ISet<ArticleTag> ArticleTags { get; set; }
public ISet<ArticleLike> ArticleLikes { get; set; }
public int Id { get; set; }
public DateTime? CreatedDate { get; set; }
public DateTime? ModifiedDate { get; set; }
[UIHint("_TagsOfArticle")]
public virtual IList<Tag> Tags { get; set; }
public virtual int NumberOfComments { get; set; }
public virtual User User { get; set; }
private IQueryable<ArticleViewModel> QueryBuilder()
{
ArticleRepository repository = new ArticleRepository();
IQueryable<ArticleViewModel> query = repository.FindAll().Select(article => new ArticleViewModel
{
Id = article.Id,
AttachmentFiles = article.AttachmentFiles.Where(a => a.ArticleId == article.Id).Distinct().ToList(),
Comments = article.Comments.Where(c => c.ArticleId == article.Id).ToList(),
CreatedDate = article.CreatedDate,
//Description = article.Description,
IsActive = article.IsActive,
IsActiveNewComment = article.IsActiveNewComment,
LikeCounter = article.LikeCounter,
ModifiedDate = article.ModifiedDate,
NumberOfComments = article.Comments.Count(c => c.ArticleId == article.Id),
RateCounter = article.RateCounter,
//Summary = article.Summary,
Tags = article.ArticleTags.Where(c => c.ArticleId == article.Id).Select(b => b.Tag).Distinct().ToList(),
Title = article.Title,
UserId = article.UserId
});
return query;
}
public virtual IQueryable<ArticleViewModel> QueryBuilderByCriteria(Expression<Func<ArticleViewModel, bool>> predicate = null, params Expression<Func<ArticleViewModel, object>>[] includeProperties)
{
IQueryable<ArticleViewModel> items = QueryBuilder();
if (includeProperties != null)
{
foreach (Expression<Func<ArticleViewModel, object>> includeProperty in includeProperties)
{
items = items.Include(includeProperty);
}
}
if (predicate != null)
return items.Where(predicate);
return items;
}
public virtual IEnumerable<ArticleViewModel> FindByCriteria(Expression<Func<ArticleViewModel, bool>> predicate = null, params Expression<Func<ArticleViewModel, object>>[] includeProperties)
{
List<ArticleViewModel> result = QueryBuilderByCriteria(predicate, includeProperties).ToList();
return result;
}
public virtual ArticleViewModel FindByArticleId(int articleId)
{
ArticleViewModel result = QueryBuilder().FirstOrDefault(a => a.Id == articleId);
return result;
}
}
答案 0 :(得分:0)
为了解决这个问题,我进行了一些改动。
1)我没有使用Tag模型,而是使用了TagViewModel
。我已经找到了Tag模型,本身就是问题的原因!我不知道为什么会这样。然后我决定创建一个简化Tag类的TagViewModel。
public class TagGridViewModel
{
public virtual int Id { get; set; }
public virtual string Title { get; set; }
public virtual string Description { get; set; }
public virtual bool? IsActive { get; set; }
public virtual int Size { get; set; }
public static List<TagGridViewModel> GetByArticleId(int articleId)
{
List<TagGridViewModel> tags = new List<TagGridViewModel>();
List<Tag> tagsPerArticle = ArticleRepository.Instance.GetTagsByArticleId(articleId);
foreach (var tag in tagsPerArticle)
{
tags.Add(new TagGridViewModel
{
Id = tag.Id,
IsActive = tag.IsActive,
Description = tag.Description,
Title = tag.Title,
Size = tag.Size
});
}
return tags;
}
}
在ArticleViewModel中:
public List<TagGridViewModel> Tags { get; set; }
在FindByCriteria方法中:
public virtual IEnumerable<ArticleViewModel> FindByCriteria(Expression<Func<ArticleViewModel, bool>> predicate = null, params Expression<Func<ArticleViewModel, object>>[] includeProperties)
{
List<ArticleViewModel> result = new List<ArticleViewModel>();
var query = QueryBuilderByCriteria(predicate, includeProperties);
foreach (var articleViewModel in query)
{
articleViewModel.Tags = TagGridViewModel.GetByArticleId(articleViewModel.Id);
articleViewModel.Owner = AppUserStore.Instance.FindByIdAsync(int.Parse(articleViewModel.UserId.ToString())).Result.UserName;
result.Add(articleViewModel);
}
return result;
}
完成这些更改后,Grid会显示数据。