0

我试图使用存储库模式和 MVC 获取数据库中提交的最后一条记录。我正在附加接口和类。以及可以放置代码的控制器。如果您需要更多详细信息,请告诉我。谢谢。

public interface IRequestRepository
{
    tblRequest GetCaseId(int caseId);     
}

public class RequestRepository: IRequestRepository
{
    helpdeskEntities context = null;
    public RequestRepository()
    {
        context = new helpdeskEntities();
    }
    public string GetCaseId(Ticket ticket)
    {
        string caseId = string.Empty;

        tblRequest tr = context.tblRequests.Where(u => u.CaseID == ticket.CaseID && u.UEmailAddress == ticket.UEmailAddress).SingleOrDefault();


        if (tr != null)
        {
            caseId = tr.CaseID;
        }

        return caseId;
    }

}

    public class Ticket
   {
    public int CaseID { get; set; }
    public string Title { get; set; }
    [Required]
    public string UFirstName { get; set; }
    [Required]
    public string ULastName { get; set; }
    //public string UDisplayName { get; set; }
    [Required]
    public string UDep_Location { get; set; }
    [Required]
    public string UEmailAddress { get; set; }
    //public string UComputerName { get; set; }
    //public string UIPAddress { get; set; }
    [Required]
    public string UPhoneNumber { get; set; }
    [Required]
    public string Priority { get; set; }
    [Required]
    public string ProbCat { get; set; }
    //public string IniDateTime { get; set; }
    //public string UpdateProbDetails { get; set; }
    //public string UpdatedBy { get; set; }
    public string InitiatedBy_tech { get; set; }
    public string AssignedBy { get; set; }
    public string TechAssigned { get; set; }
    [Required]
    [DataType(DataType.MultilineText)]
    public string ProbDetails { get; set; }


}



Controller
 public ActionResult CreateTicket(tblRequest td)
    {
    }
4

1 回答 1

1

首先,您需要升级您的 IRequestRepository 并添加该方法:(我假设您为此使用 EntityFramework)

   public IRequestRepository 
   {
       Request Latest(Ticket ticket);
   }

接下来,您需要在您的RequestRepository

   public class RequestRepository : IRequestRepository
   {
        /* other code here */

        public Request Latest(Ticket ticket)
        {
           // I'm also assuming you're using an auto incremented CaseId
           return this.context.tblRequests.OrderByDescending(p => p.CaseId).FirstOrDefault(p => p.UEmailAddress == ticket.UEmailAddress);
        }
    }

还有一件事:

您的 IRequestRepository.GetCaseId 实现返回一个字符串,而它应该返回一个tblRequest(人们也希望它返回一个 int Id ...)

无论如何,我希望这会有所帮助!

于 2013-06-04T17:57:33.010 回答