我试图得到最后的记录在数据库中提交使用存储库模式和MVC

本文关键字:提交 存储 MVC 模式 数据库 最后的 记录 | 更新日期: 2023-09-27 18:16:49

我试图获得在数据库中使用存储库模式和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)
    {
    }

我试图得到最后的记录在数据库中提交使用存储库模式和MVC

首先,你需要升级你的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…)

无论如何,我希望这有助于!