0

リポジトリ パターンと MVC を使用して、db に送信された最後のレコードを取得しようとしています。インターフェイスとクラスをアタッチしています。コードを配置できるコントローラー。詳細が必要な場合はお知らせください。ありがとう。

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 に答える