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
//public virtual IEnumerable
public virtual ICollection
public virtual ICollection
//public virtual ICollection
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
this.Functions = new List
//this.Tenures = new List
}//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
}
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:
|
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!!
albert albertPosted Jun 24, 2012, 2:58 PM
Suthish NairPosted Jun 24, 2012, 2:08 PM