代码之家  ›  专栏  ›  技术社区  ›  matty

asp。net 4.5编码第一个类并在Listview中显示项

  •  0
  • matty  · 技术社区  · 8 年前

    我创建了以下类,然后使用实体优先的方法成功创建了数据库:

    using System;
    using System.Collections.Generic;
    
    using System.ComponentModel.DataAnnotations;
    
    namespace HR_Test_v0_1.Models
    {
        public class simStaff
        {
            [Key]
            public int simStaffId { get; set; }
            [Required(ErrorMessage ="You must enter a first name")]
            public string FirstName { get; set; } 
            [Required(ErrorMessage ="You must enter a last name")]
            public string LastName { get; set; }
    
            public simStaff simHoursRecorded { get; set; }
            public simPayRate simPayRate { get; set; }
        }
    
        public class simHoursRecorded
        {
            [Key]
            public int simHoursRecordId { get; set; }
            [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
            public DateTime DayWorked { get; set; }
            public int HoursBooked { get; set; }
            public bool AuthorisedToPay { get; set; }
    
            public int simStaffId { get; set; }
            public ICollection<simStaff> simStaff { get; set; }
    
        }
    
        public class simPayRate
        {
            [Key]
            public int simPayRateId { get; set; }
            public double RatePerHour { get; set; }
            public DateTime DateAppliesFrom { get; set; }
    
            public int simHoursTypeId { get; set; }       
            public ICollection<simHoursType> simHoursType { get; set; }
    
            public int simStaffId { get; set; }
            public ICollection<simStaff> simStaff { get; set; }
        }
    
        public class simHoursType
        {
            [Key]
            public int simHoursTypeId { get; set; }
            public string HoursType { get; set; }
            public simPayRate simPayRate { get; set; }
        }
    
    }
    

    我的上下文类如下:

    public class EFContext : DbContext
    {
        public EFContext() : base("name=HRTestContext")
        {
    
        }
    
        public DbSet<simStaff> simStaffs { get; set; }
        public DbSet<simHoursRecorded> simHoursRecordeds { get; set; }
        public DbSet<simPayRate> simPayRates { get; set; }
        public DbSet<simHoursType> simHoursType { get; set; }
    
    }
    

    以及我的存储库中的相关摘录。cs文件如下:

        public IEnumerable<simPayRate> GetPayRate
        {
            get
            {
                return context.simPayRates;
            }
    
        }
    

    在ListView中,我有下面的代码。然而,当我输入Item时,InsertItemTemplate在ItemTemplate中工作。simHoursType,我希望看到simHoursType实体中的HoursType选项:

    <ItemTemplate>
    <tr>
     <td><%# Item.RatePerHour %></td>
     <td><%# Item.DateAppliesFrom.ToShortDateString() %></td>
     <td><%# Item.simHoursType %></td>
    
     <td>
     <asp:Button CommandName="Edit" Text="Edit" runat="server" />
     </td>
     </tr>
     </ItemTemplate>
       <InsertItemTemplate>
        <tr>
        <td><input id="RatePerHour" runat="server" 
               value="<%# BindItem.RatePerHour %>" /></td>
        <td><input id="DateAppliesFrom" runat="server" 
               value="<%# BindItem.DateAppliesFrom %>" /></td>
        <td>
            <asp:DropDownList ID="ddHoursType" 
                            runat="server"
                            AppendDataBoundItems="true"
                            SelectMethod="GetHoursType" 
                            DataTextField="HoursType"
                            DataValueField="simHoursTypeId"
                            SelectedValue="<%# BindItem.simHoursTypeId %>"
                            AutoPostBack="false" />
         </td>
         <td>
           <asp:Button CommandName="Insert" runat="server" text="Add" />
           <asp:Button CommandName="Cancel" runat="server" Text="Cancel" />
         </td>
         </tr>
         </InsertItemTemplate>
    

    我是否可以更改创建类的方式,以便输入Item。simHoursType。小时类型,如果不是,我应该如何显示小时类型?

    2 回复  |  直到 8 年前
        1
  •  0
  •   trix    8 年前

    看起来你不能这样做的原因是 Item.simHoursType 实际上是 ICollection 属于 simHoursType . 为了访问 HoursType 您必须访问该集合中的单个项目。例如: Item.simHoursType[0].HoursType .

        2
  •  0
  •   matty    8 年前

    代码优先约定不正确,应如下所示:

    public class simStaff
    {
        [Key]
        public int simStaffId { get; set; }
        [Required(ErrorMessage ="You must enter a first name")]
        public string FirstName { get; set; }
        [Required(ErrorMessage ="You must enter a last name")]
        public string LastName { get; set; }
    
        public ICollection<simHoursRecorded> simHoursRecorded { get; set; }
        public ICollection<simPayRate> simPayRate { get; set; }
    }
    
    public class simHoursRecorded
    {
        [Key]
        public int simHoursRecordId { get; set; }
        [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
        public DateTime DayWorked { get; set; }
        public int HoursBooked { get; set; }
        public bool AuthorisedToPay { get; set; }
    
        public int simStaffId { get; set; }
        public simStaff simStaff;
    }
    
    public class simPayRate
    {
        [Key]
        public int simPayRateId { get; set; }
        public double RatePerHour { get; set; }
        public DateTime DateAppliesFrom { get; set; }
    
        public int? simHoursTypeId { get; set; }       
        public simHoursType simHoursType { get; set; }
        public int simStaffId { get; set; }
        public simStaff simStaff;
    }
    
    public class simHoursType
    {
        [Key]
        public int simHoursTypeId { get; set; }
        public string HoursType { get; set; }
    
        public ICollection<simHoursType> simPayRate { get; set; }
    }
    

    然后,有必要在存储库中使用include。cs如下:

        public IEnumerable<simPayRate> GetPayRate
        {
            get
            {
                return context.simPayRates.Include("simHoursType");
            }
    
        }