简体   繁体   中英

How to use repository in Asp.net MVC

I'm trying to get the id number of an item an update that item. However at the moment this is what I have. Not sure how to do the issuerepository. I have updated the question and getting this error Severity Code
'Employee' does not contain a definition for 'Entry' and no extension method 'Entry' accepting a first argument of type 'Employee' could be found (are you missing a using directive or an assembly reference?)

And this error An object reference is required for the non-static field, method, or property Issue getIssue = IssueRepository.GetEmployeeById(id);

Employee Class

 public class Employee
{


    [Required]
    [Display(Name="Employee Number")]
    public int employeeNum { get; set; }

    [Display(Name = "Employee First Name")]
    public string firstName { get; set; }

    [Display(Name = "Employee Last Name")]
    public string lastName { get; set; }

    [Display(Name = "Employee Department")]
    public string department { get; set; }

    [Display(Name = "Employee Name")]
    public string Name
    {
        get
        {



            return string.Concat(firstName, " ", lastName);
        }

    }


    [Display(Name = "Logged in as:")]
    public string LoggedInUserName { set; get; }
    [Display(Name = "Approved By:")]
    public string ApprovedByName { set; get; }



  public class IssueRepository : IIssueRepository, IDisposable
{
    private Employee context;

    public IssueRepository(Employee context)
    {
        this.context = context;
    }

    public Employee GetEmployeeById(int id)
    {
        return context.employeeNum.Find(id);
    }

    public void UpdateEmployee(Employee employee)
    {
        context.Entry(employee).State = EntityState.Modified;
    }

    public void Save()
    {
        context.SaveChanges();
    }
} 

public class IIssueRepository {

    public interface IssueRepository : IDisposable
    {
        Employee GetEmployeeById(int employeeNum);
        void UpdateEmployee(Employee employee);

        void Save();
    }
} 

public ActionResult Edit(int id) {

        Issue getIssue = IssueRepository.GetEmployeeById(id);
        return View(getIssue);


    }


    /// <summary>
    /// Gets the changes submitted from the user and updates the Item in the List  
    /// </summary>
    /// <param name="issue"></param>
    /// <returns></returns>
    /// 

    [HttpPost]
    public ActionResult Edit(Issue issue)
    {


        if (ModelState.IsValid)

        {

            _issueRepository.UpdateEmployee(issue);

            _issueRepository.Save();

            return RedirectToAction("IssueItem", issue);
        }


        else
        {
            ModelState.AddModelError(string.Empty, "Please make sure you have filled in all required fields.");
        }

            return View(issue);


        }

Start with a class called IIssueRepository:

  public interface IIssueRepository : IDisposable
    {
        Employee GetEmployeeById(int employeeNum);
    }

Create a class called IssueRepository:

  public class IssueRepository : IIssueRepository
    {
        public Employee GetEmployeeById(int employeeNum)
        {
            // go get the data and return an employee object
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM