albert albert

albert albert

  • NA
  • 524
  • 0

one-to-one relationship in asp.net mvc3

Jun 13 2012 6:21 AM
Hi everybody,

 I have this:

Model:Employee:

[code]
 public class Employee
    {
        public int EmployeeID  {get; set; }
        [Required]
        [StringLength(50)]
        [Display(Name = "Voornaam")]
        public string Name { get; set; }
        [Required]
        [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Verkeerd e-mail ingetikt")]
        public string Email { get; set; }

        [DisplayFormat(DataFormatString = "{0:c}")]
        //[Required(ErrorMessage = "U moet wel een uurloon opgeven")]
        //[Column(TypeName = "uurloon")]
        public decimal? HourlyWage { get; set; }

        public bool Active { get; set; }

        public virtual ICollection<Project> Projects { get; set; }
        //public virtual IEnumerable<Activity> Activity { get; set; }
        public virtual ICollection<Activity> Activity { get; set; }
        public virtual ICollection<Function>Functions  { get; set; }
        //public virtual ICollection<Tenure> Tenures { get; set; }

        private Tenure tenure;
        public virtual Tenure Tenure
        {
            get { return tenure; }
        }

        public  void setTenu(Tenure tenure)
        {
            this.tenure = tenure;
            tenure.SetEmployee(this);
        }


        public Employee()
        {
            this.Projects = new List<Project>();
            this.Functions = new List<Function>();
            //this.Tenures = new List<Tenure>();
       
        }//end constructor



    }
[/code]

and class: Tenue:

[code]
public class Tenure
    {
        public int TenureID { get; set; }
        public string Name { get; set; }

        private Employee employee;
        public virtual Employee Employee
        {
            get { return employee; }
        }

        public  Tenure()
        {
            //this.employee = new List<Employee>();
        }

        public  void SetEmployee(Employee employee)
        {
            this.employee = employee;
            this.employee.setTenu(this);
        }
    }
[/code]

and the controller: Employee:

[code]
 public class EmployeeController : Controller
    {
        private TimeSheetContext db = new TimeSheetContext();

        //
        // GET: /Employee/

        public ViewResult Index()
        {
            var employees = db.Employees.Include(t => t.Tenure);
            return View(db.Employees.ToList());
        }

        //
        // GET: /Employee/Details/5

        public ViewResult Details(int id)
        {
            Employee employee = db.Employees.Find(id);
            return View(employee);
        }

        //
        // GET: /Employee/Create

       
        public ActionResult Create()
        {
            ViewBag.TenureID = new SelectList(db.Tenures, "TenureID", "Name");
            return View();
        }
         



        //
        // POST: /Employee/Create

        [HttpPost]
        public ActionResult Create(Employee employee)
        {
            try
            {




                if (ModelState.IsValid)
                {
                    db.Employees.Add(employee);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch(DataException)
            {
                ModelState.AddModelError("", employee.Tenure.Name.ToString());
            }


            ViewBag.TenureID = new SelectList(db.Tenures, "TenureID", "Name", employee.Tenure);
            return View(employee);
        }

      
       
        //
        // GET: /Employee/Edit/5
 
        public ActionResult Edit(int id)
        {
            Employee employee = db.Employees.Find(id);
            ViewBag.TenureID = new SelectList(db.Tenures, "TenureID", "Name", employee.Tenure.TenureID);
            return View(employee);
        }

        //
        // POST: /Employee/Edit/5

        [HttpPost]
        public ActionResult Edit(Employee employee)
        {
            if (ModelState.IsValid)
            {
                db.Entry(employee).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.TenureID = new SelectList(db.Tenures, "TenureID", "Name", employee.Tenure.TenureID);
            return View(employee);
        }

        //
        // GET: /Employee/Delete/5
 
        public ActionResult Delete(int id)
        {
            Employee employee = db.Employees.Find(id);
            return View(employee);
        }

        //
        // POST: /Employee/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {           
            Employee employee = db.Employees.Find(id);
            db.Employees.Remove(employee);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
[/code]

I have a dropdownlist, where the user can select: fulltime, parttime.

But If I try to commit to the Database the textbox: Dienstverband is empty.

And if I try then Edit I get the error:

[code]

Server Error in '/' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 82:         { Line 83:             Employee employee = db.Employees.Find(id); Line 84:             ViewBag.TenureID = new SelectList(db.Tenures, "TenureID", "Name", employee.Tenure.TenureID); Line 85:             return View(employee); Line 86:         }

Source File: F:\WinituProjects07-06-2012\ManyToMany26-05-2012WorkingVersionWorkingVersion02 - Copy\ManyToMany\Controllers\EmployeeController.cs    Line: 84

[/code]

And that is logical, because there is no value. But how u commit the value to the database?

THX!!




Answers (2)