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

MVC核心:基于主键显示另一个属性

  •  0
  • coolhand  · 技术社区  · 7 年前

    public class Waiver
        {
            public int WaiverID { get; set; }
            [Required(ErrorMessage = "Please select an Office")]
            [Range(1, int.MaxValue, ErrorMessage = "Please select an Office")]
            public int OfficeID { get; set; }
            //...other properties
    }
    

     IEnumerable<Waiver> waiverList = repository.Waivers
                    .Where(wl => waiverNum == 0 || wl.WaiverID == waiverNum)
                    .Where(wl => officeId == 0 || wl.OfficeID == officeId)
    //...other search criteria
    

    @model IEnumerable<Waiver>
    
    @foreach (var item in Model)
        {
            <tr>
                <td class="text-right">@item.WaiverID</td>
                <td class="text-right">@item.OfficeID></td>
    @* other display columns *@
    

    虽然传递给视图的弃权对象具有OfficeID属性,但我希望在Office模型中显示Name属性,因为它对用户更有用。我怎样才能做到这一点?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace Waiver.Models
    
    //This class implements the IWaiverRepository and gets its data using Entity Framework Core
    {
        public class EFWaiverRepository : IWaiverRepository
        {
    
    
            private ApplicationDbContext context;
    
            public EFWaiverRepository(ApplicationDbContext ctx)
            {
                context = ctx;
            }
    
    
            public IEnumerable<Waiver> Waivers => context.Waivers;
            public IEnumerable<Office> Offices => context.Offices;
    
            //*****Waiver Methods*****
            public void SaveWaiver(Waiver waiver)
            {
                //if the waiver number is 0, create a new waiver
                if (waiver.WaiverID == 0)
                {
                    context.Waivers.Add(waiver);
                }
                //if there wavier number exists, save the changes to the database
                else {
                    Waiver dbEntry = context.Waivers.FirstOrDefault(w => w.WaiverID == waiver.WaiverID);
                    if (dbEntry != null)
                    {
                        dbEntry.Requestor = waiver.Requestor;
                        dbEntry.RequestorEmail = waiver.RequestorEmail;
                        dbEntry.OfficeID = waiver.OfficeID;
                        dbEntry.RequestDate = waiver.RequestDate;
                        dbEntry.System = waiver.System;
                        dbEntry.Source = waiver.Source;
                        dbEntry.Requirement = waiver.Requirement;
                        dbEntry.MitigationPlan = waiver.MitigationPlan;
                        dbEntry.FinalAssessment = waiver.FinalAssessment;
                        dbEntry.Status = waiver.Status;
                        //get the signatures
                        dbEntry.PmSignature = waiver.PmSignature;
                        dbEntry.PmSignDate = waiver.PmSignDate;
                    }
                }
                context.SaveChanges();
            }
    
            public Waiver DeleteWaiver(int waiverID)
            {
                Waiver dbEntry = context.Waivers
                    .FirstOrDefault(w => w.WaiverID == waiverID);
                if (dbEntry != null)
                {
                    context.Waivers.Remove(dbEntry);
                    context.SaveChanges();
                }
                return dbEntry;
            }
    
            //*****Office Methods*****
            public void SaveOffice(Office office)
            {
                //if the ID=0, its a new entry. Add to db
                if (office.OfficeID == 0)
                {
                    context.Offices.Add(office);
                } else
                {
                    Office dbEntry = context.Offices.FirstOrDefault(o => o.OfficeID == office.OfficeID);
                    if (dbEntry != null)
                    {
                        dbEntry.Name = office.Name;
                        dbEntry.SiteID = office.SiteID;
                    }
                }
                context.SaveChanges();
            }
    
            public Office DeleteOffice(int officeID)
            {
                Office dbEntry = context.Offices
                    .FirstOrDefault(o => o.OfficeID == officeID);
                if (dbEntry != null)
                {
                    context.Offices.Remove(dbEntry);
                    context.SaveChanges();
                }
                return dbEntry;
            }
    

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace Waiver.Models
    {
        public interface IWaiverRepository
        {
            IEnumerable<Waiver> Waivers { get; }
            IEnumerable<Office> Offices { get; }
            IEnumerable<Site> Sites { get; } 
    
            void SaveWaiver(Waiver waiver );
            Waiver DeleteWaiver(int waiverID);
    
            void SaveOffice(Office office);
            Office DeleteOffice(int officeID);
        }
    }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   DSR    7 年前

    EF Core尚不可能实现延迟加载,因此需要包含相关实体。解决方案如下所示。

    public class Waiver
    {
        public int WaiverID { get; set; }
        [Required(ErrorMessage = "Please select an Office")]
        [Range(1, int.MaxValue, ErrorMessage = "Please select an Office")]
        public int OfficeID { get; set; }
    
        public virtual Office Office { set;get;}
    }
    

    repository.Waivers

    已更新

    IEnumerable<Waiver> waiverList = repository.Waivers.Where(wl => waiverNum == 0 || wl.WaiverID == waiverNum).Where(wl => officeId == 0 || wl.OfficeID == officeId)
    

    //...其他搜索条件

    @model IEnumerable<Waiver>
    <table>
    @foreach (var item in Model)
    {
        <tr>
            <td> @item.WaiverID </td>
            <td> @item.OfficeID </td>
            <td> @item.Office.Name </td>
        </tr>
    }
    </table>
    

    使现代化

    改变 public IEnumerable<Waiver> Waivers => context.Waivers;

    public IEnumerable<Waiver> Waivers => context.Waivers.Include(o => o.Office);
    

    如果这不起作用,则需要检查db上下文类并正确设置之间的映射 Waivers Office

        2
  •  0
  •   Shyju    7 年前

    添加 virtual Office 类型

    public class Waiver
    {
        public int WaiverID { get; set; }
        [Required(ErrorMessage = "Please select an Office")]
        [Range(1, int.MaxValue, ErrorMessage = "Please select an Office")]
        public int OfficeID { get; set; }
    
        public virtual Office Office { set;get;}
    }
    

    @model IEnumerable<Waiver>
    <table>
    @foreach (var item in Model)
    {
        <tr>
            <td> @item.WaiverID </td>
            <td> @item.OfficeID </td>
            <td> @item.Office.Name </td>
        </tr>
    }
    </table>
    

    EF将为您的豁免项目加载Office属性。