• ASP.NET MVC 2 学习笔记一: 两个表的连接的信息显示


     

    ASP.NET MVC 2 学习笔记一: 两个表的连接的信息显示

     

     

     

      最近在写一个关于公司内部的请假系统,要用到ASP.NET MVC,于是乎一边学习,一边写代码.下面写写在学习的时候遇到的问题,以及解决办法,留作学习沉淀.

      关于模型类的创建就不说了,下面是我用Entity Framework创建的,取名StaffDataModel.edmx,如下图:

      

      上图中有三个表,当做到经理角色时,要显示所有未批准的请假信息,其中所需信息大部分在LeavingInformaion这个表中,但是申请人的英文名字信息在StaffList中,两个表关系是一对多,通过员工Id连接.现在,需要在View视图中显示这些信息,用的是ViewData.Model传的值.这样,仅用ADO.NET Entity Data Model生成的模型类是不够的.

      所以,考虑在Models层创建了一个模型类LeavingInfoModel.cs,代码如下:

      

     1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Web;
    5 using System.Web.Mvc;
    6 using System.ComponentModel;
    7
    8 namespace MvcLeavingApp.Models
    9 {
    10 /// <summary>
    11 /// 请假信息
    12 /// </summary>
    13 public class LeavingInfoModel
    14 {
    15 public int infoid;
    16 public string enname;
    17 public DateTime starttime;
    18 public DateTime requesttime;
    19 public int relativetime;
    20 public DateTime endtime;
    21
    22 public LeavingInfoModel()
    23 {
    24 infoid = 0;
    25 enname = null;
    26 starttime = DateTime.Now;
    27 requesttime = DateTime.Now;
    28 relativetime = 0;
    29 endtime = DateTime.Now;
    30 }
    31 }
    32 }

      在LeavingInfoModel这个模型类中,定义了所需要显示的所有字段,并在构造函数中为所有字段赋初值.

     

      那么在Controllers层,对LeavingInformaion和StaffList两个表左连接后,用linq中的Lambda表达式获得符合筛选条件的结果集合,在创建一个对象数组,赋予结果集合,最后传给ViewData.Model.其中 ManagerController.cs的代码如下:

      

     1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Web;
    5 using System.Web.Mvc;
    6 using MvcLeavingApp.Models;
    7
    8 namespace MvcLeavingApp.Controllers
    9 {
    10 [HandleError]
    11 public class ManagerController : Controller
    12 {
    13 StaffDataEntities _Staffdb;
    14
    15 public ManagerController()
    16 {
    17 _Staffdb = new StaffDataEntities();
    18 }
    19      /// <summary>
    20      /// 查看带审批的请假单
    21      /// </summary>
    22      /// <returns></returns>
    23 public ActionResult ApproveLeave()
    24 {
    25 var approveInfor = from stafflist in _Staffdb.StaffList
    26 from leavinginfo in stafflist.LeavingInformation
    27 where leavinginfo.IfApprove == 0
    28 select new
    29 {
    30 infoid = leavinginfo.InfoId,
    31 starttime = leavinginfo.StartTime,
    32 endtime = leavinginfo.EndTime,
    33 relativetime = leavinginfo.RelativeTime,
    34 requesttime = leavinginfo.RequestTime,
    35 requestname = stafflist.EnName
    36 };
    37
    38 LeavingInfoModel[] leaveinfomodel = new LeavingInfoModel[approveInfor.Count()];
    39 int itemindex = 0;
    40
    41 foreach (var info in approveInfor)
    42 {
    43 leaveinfomodel[itemindex] = new LeavingInfoModel();
    44 leaveinfomodel[itemindex].infoid = info.infoid;
    45 leaveinfomodel[itemindex].enname = info.requestname;
    46 leaveinfomodel[itemindex].starttime = info.starttime;
    47 leaveinfomodel[itemindex].endtime = info.endtime;
    48 leaveinfomodel[itemindex].relativetime = info.relativetime;
    49 leaveinfomodel[itemindex].requesttime = info.requesttime;
    50 itemindex++;
    51 }
    52
    53 ViewData.Model = leaveinfomodel.ToList();
    54 return View();
    55 }
    56 }

      可以看到,在ViewData.Model 中已经有了我们所需要的符合条件的结果集.

      所以,在View视图中我们可以这样写:

      

     1 <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/SiteManager.Master" Inherits="System.Web.Mvc.ViewPage<List<MvcLeavingApp.Models.LeavingInfoModel>>" %>
    2
    3 <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    4 ApprovingLeave
    5 </asp:Content>
    6
    7 <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    8 <script src="http://www.cnblogs.com/Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    9 <script type="text/javascript">
    10 $(function () {
    11 $("tr:even").css ({ "background-color": "#CEDBE8" });
    12 });
    13 </script>
    14
    15 <h2>Required to Approve Leave</h2>
    16 <form method="post" action="/Manager/ApproveLeave">
    17 <div>
    18 <fieldset>
    19 <legend>
    20 Require to Approve Absence Form</legend>
    21 <table>
    22 <thead>
    23 <tr>
    24 <td align="center">
    25 If/Not Approved
    26 </td>
    27 <td align="center">
    28 Start Time
    29 </td>
    30 <td align="center">
    31 End Time
    32 </td>
    33 <td align="center">
    34 Relative Time
    35 </td>
    36 <td align="center">
    37 Request Time
    38 </td>
    39 <td align="center">
    40 Request Staff
    41 </td>
    42 <td align="center">
    43 Details
    44 </td>
    45 </tr>
    46 </thead>
    47 <tbody>
    48 <% foreach (var m in ViewData.Model)
    49 { %>
    50 <tr>
    51 <td align="center">
    52 <%=Html.CheckBox(""+m.infoid) %>
    53 </td>
    54 <td align="center">
    55 <%=m.starttime.ToShortDateString() %>
    56 </td>
    57 <td align="center">
    58 <%=m.endtime.ToShortDateString() %>
    59 </td>
    60 <td align="center">
    61 <%=m.relativetime %>
    62 days
    63 </td>
    64 <td align="center">
    65 <%=m.requesttime.ToShortDateString() %>
    66 </td>
    67 <td align="center">
    68 <%=m.enname %>
    69 </td>
    70 <td align="center">
    71 <%= Html.ActionLink("Details", "LeavingDetails", new { infoid = m.infoid })%>
    72 </td>
    73 </tr>
    74 <% } %>
    75 </tbody>
    76 </table>
    77 <p>
    78 <input type="submit" value="Approved" />
    79 </p>
    80 </fieldset>
    81 </div>
    82 </form>
    83 </asp:Content>

      其中,页面传值使用System.Web.Mvc.ViewPage<List<MvcLeavingApp.Models.LeavingInfoModel>>来接收,这样我们就可以在ViewData.Model中使用m.enname来得到我们所需要的另一个表中的enname英文名信息了。

     

      这样就可以实现两个表的连接,显示两个表的所有信息了。

     

      Ps:这篇学习笔记同各位分享,并积累一下在ASP.NET MVC 2的学习中所遇到的一系列问题,留作项目沉淀,希望对你有所帮助。第一次写博客,写得不好,还请见谅,如有问题,敬请指出,不胜感激!

     

      

     

    Tweet

  • 相关阅读:
    weixueyuanNginx日志管理与监控8
    w3cschoolNetty 实战精髓篇2
    weixueyuanNginx集群9
    Java IO之 Netty与NIO服务器NIO中的零拷贝
    weixueyuanNginx在Kubernetes10
    CString内容转为16进制unsigned char
    为 Windows 驱动编译静态 lib 库
    makefile编译静态库或动态库,遍历所有源文件
    线程安全
    三十六、Promise
  • 原文地址:https://www.cnblogs.com/MarkRao/p/Mark_Rao.html
Copyright © 2020-2023  润新知