-1

我想从数据库中计算每个用户的总叶子。并希望在Asp.Net MVC-3 (Aspx)的 Telerik gridview 中显示它。

我有一个两列的gridview。第一列显示所有员工/用户的姓名,第二列将显示该用户的总休假。

我的数据库如下

leave_master

leave_id(主键) user_id(外键与表 user_master 的 user_id) leave_days

用户主

user_id(主键) user_real_name

我的模型文件如下

名称 - LeaveGridview.cs

<pre>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;


namespace ProjectManagementSystem.Models
{
    public class LeaveGridview
    {

        public int user_id { get; set; }

        public string user_name { get; set; }

        public string user_real_name { get; set; }    

        public int leave_id { get; set; }

        public decimal leave_days { get; set; }


    }
}
</pre>

第二个文件

名称 - LeaveGridviewFetch.cs

<pre>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ProjectManagementSystem.Models
{

    public class LeaveGridviewFetch
    {



        public static IList&lt;LeaveGridview&gt; all()
        {
            IList&lt;LeaveGridview&gt; result =
                (IList&lt;LeaveGridview&gt;)HttpContext.Current.Session["leave1"];

            if (result == null)
            {

                HttpContext.Current.Session["leave1"] = result =
                    (from l in new ProjectManagementSystemEntities3().leave_master
                     select new LeaveGridview
                     {
                         user_id = l.user_id,
                         leave_id = l.leave_id,
                         user_real_name = l.user_master.user_real_name,



                     }).ToList();

            }
            return result;
        }
    }
}
</pre>

我的控制器文件名LeaveController.cs

<pre>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ProjectManagementSystem.Models;
using Telerik.Web.Mvc;

namespace LeaveModule.Controllers
{
    public class LeaveController : Controller
    {
        public ProjectManagementSystemEntities3 pb = new ProjectManagementSystemEntities3();

  public ActionResult LeaveShow()
        {
            var leave_master = pb.leave_master.Include("user_master");
            ViewBag.leave_id = new SelectList(pb.user_master, "user_id", "user_real_name");
            return View(leave_master.ToList());
        }

 [GridAction]
        public ActionResult _SelectBatchEditing()
        {
            return View(new GridModel(LeaveGridviewFetch.all()));
        }

}
}

</pre>

我的看法是

名称LeaveShow.ascx

<pre>

&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %&gt;

&lt;asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"&gt;
    LeaveShow
&lt;/asp:Content&gt;

&lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"&gt;

&lt;%  Html.Telerik().Grid&lt;ProjectManagementSystem.Models.LeaveGridview&gt;() 
                       .Name("grid")
                       .DataKeys(keys =&gt;
                       {
                           keys.Add(l =&gt; l.leave_id);
                       })

                       .DataBinding(dataBinding =&gt;
                        dataBinding.Ajax()
                       .Select("_SelectBatchEditing", "Leave") // Leave is controller name
                       )

                        .Columns(columns =&gt;
                       {

                           columns.Bound(l =&gt; l.user_real_name).Width(200); 

                           columns.Bound(l =&gt; l.leave_days).Width(150);

                       })


                       .Groupable()
                       .Pageable()
                       .Filterable()
                       .Render();

                       %&gt;


&lt;/asp:Content&gt;


</pre>

我想像这样输出

名字---------------total_叶

abc---------------- 10

xyz---------------- 12

pqr---------------- 06

那么我该怎么做呢?

提前致谢...

4

1 回答 1

0

leave_id如果要聚合天数,则不能在查询中包含 。我还将模型中leave_days的名称更改为. 我假设您的对象上有一个名为. 将 LeaveGridFetch 中的查询更改为:LeaveGridViewtotal_leave_daysIEnumerable<leave_master>user_masterleave_masters

        HttpContext.Current.Session["leave1"] = result =
                (from l in new ProjectManagementSystemEntities3().user_master
                 select new LeaveGridview
                 {
                     user_id = l.user_id,
                     user_real_name = l.user_real_name,
                     total_leave_days = l.leave_masters.Sum(i => i.leave_days)
                 }).ToList();
于 2012-04-13T08:59:17.620 回答