1

在我的 MVC5 应用程序中,我有一个表格,可以说它的名字是学生,每个学生都属于一个团队。我想按百分比向学生展示他们的团队。出于这个原因,我还创建了一个 ViewModel 以便将Student实体与Team实体连接起来。那么,如何在 Kendo UI 饼图中显示我想要的数据呢?你能看一下代码并纠正下面的错误吗?您能否提供适合这种方法的 View 示例?提前致谢。


视图模型:

public class StudentViewModel
{
    public int StudentId { get; set; }

    public int TeamId { get; set; }

    public string TeamName { get; set; }

    public int TeamPercentage { get; set; }
}


控制器:

public ActionResult Index_Read([DataSourceRequest] DataSourceRequest request)
{
    var dataContext = repository.Student;

    var result = dataContext.ToDataSourceResult(request, m => new StudentViewModel
        {
            StudentId = m.StudentId,
            TeamId = m.TeamId,
            TeamName = m.TeamName,

            TeamPercentage = //??? How can I obtain percentage by Lambda Expression ???
        }
    );
    return Json(result, JsonRequestBehavior.AllowGet);
}
4

1 回答 1

1

你可以这样做:

视图模型

public class StudentViewModel
{
    public string category { get; set; }
    public int value { get; set; }
}

控制器(注意我在查询中做了一些假设)

public ActionResult Index_Read()
{
    var teamCount = repository.Team.Count();
    var result = repository.Student.GroupBy(g => g.TeamName).Select(s => new StudentViewModel { category = s.Key, value = s.Count()/teamCount });
    // Imagine result contains something like this: { category = "Team 1", value = "20" }, { category = "Team 2", value = "30" }, { category = "Team 3", value = "10" }, { category = "Team 4", value = "40" }
    return View(result);
}

看法

@model IEnumerable<StudentViewModel>

@(Html.Kendo().Chart()
    .Name("chart")
    .Title(title => title.Text("Teams").Position(ChartTitlePosition.Bottom))
    .Legend(legend => legend.Visible(true))
    .Series(series => series.Pie(Model))
)

选择

另一种方法是使用数据源绑定。

public class StudentViewModel
{
    public string TeamName{ get; set; }
    public int Percentage { get; set; }
}

@(Html.Kendo().Chart<StudentViewModel>()
    .Name("chart2")
    .DataSource(ds => ds.Read("GetTeamData", "Home"))
    .Series(series => series.Pie(m => m.Percentage, model => model.TeamName))
)
于 2015-03-09T03:32:13.000 回答