English 中文(简体)
如何在MVC-3中分别计算每个用户的请假总数(从数据库中抽取)?
原标题:How to count total of leaves(fetching from database) of each user separately in MVC-3?

I wants to count the total leaves of each users from the database. and wants to display it in telerik gridview in Asp.Net MVC-3 (Aspx).

i 有两个栏的电网。 第1栏列出所有雇员/用户的名称,第2栏将显示这些用户的总休假。

页: 1 页: 1

leave_id (primary key) user_id (foreign key with user_id of table user_master) leave_days

user_master/b>

user_id (primary key) 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>

第2次档案

姓名: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>

My contgroller file name 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>

我的看法

页: 1

<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>

i 大麻产量

页: 1 10

页: 1 12

页: 1 页: 1

因此,如何做到这一点?

预告......

问题回答

You can t have the leave_id in your query if you want to aggregate the days. I would also change the name leave_days in the LeaveGridView model to total_leave_days. I assume that there is an IEnumerable<leave_master> on your user_master object called leave_masters. Change the query in LeaveGridFetch to:

        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();




相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...