Hi Everybody,
I try to commit the data from 2 dropdownlist to the database.
I have 2 dropdownlists: Projects and Activities.
In the view: u can select a project and in the second dropdownlist u can select the associated activities. But when I want to commit it to the database. I doesnt do.
I put a breakpoint on this line:
[code]
if (db.Activities != null)
{
ViewBag.Activities = db.Activities.ToList();
}
return View(hour);
[/code]
and it says:
Local = Count = 0
So ID is 0. But I dont know where to fix it. There is no reference.
Here is the code:
models:
[code]
public class Project
{
//[DatabaseGenerated(DatabaseGeneratedOption.None)]
// [Display(Name= "Number")]
[Key]
public int ProjectID { get; set; }
// [Display(Name = "Naam")]
public string Name { get; set; }
// [StringLength(100,ErrorMessage = "U kunt niet meer dan 100 tekens invoeren!! ")]
[Display(Name ="omschrijving" )]
public string Description { get; set; }
[Display(Name = "Geplande uren")]
public decimal PlannedHours { get; set; }
[DisplayFormat(DataFormatString = "{0:c}")]
[Display(Name = "Prijs")]
public decimal price { get; set; }
public bool Intern { get; set; }
[Display(Name = "project manager")]
public string projectManager { get; set; }
[Display(Name = "Project Code")]
public string projectCode { get; set; }
[Display(Name = "Start Datum")]
public DateTime? StartDate { get; set; }
[Display(Name = "Eind datum")]
public DateTime? EndDate { get; set; }
[Display(Name = "Actief?")]
public bool Active { get; set; }
// public StatusProject StatusProject { get; set; }
public virtual ICollection
Employees { get; set;} //public virtual ICollectionAcitivity { get; set; } public IEnumerable Activities { get; set; } public Project() { this.Employees = new List(); } } [/code] Activity: [code] public class Activity { //[Key] //[DatabaseGenerated(DatabaseGeneratedOption.Identity)] //[Display(Name = "Nummer(willekeurig nummer)")] //[Key] public int ActivityID { get; set; } [MaxLength(50),Required(ErrorMessage="Naam moet wel ingevuld worden")] [Display(Name = "Activiteit")] public string Name { get; set; } [Display(Name = "Omschrijving")] public string Description { get; set; } [Display(Name = "uren")] public decimal Hour { get; set; } [DataType(DataType.Date)] [Display(Name = "Begin datum")] public DateTime BeginDate { get; set; } [DataType(DataType.Date)] [Display(Name = "Eind datum")] public DateTime EndDate { get; set; } [Display(Name = "Tarief")] public decimal Tariff { get; set; } [Display(Name = "Actief?")] public bool Active { get; set; } [Display(Name = "project")] public int ProjectID { get; set; } //public virtual DbSet Project { get; set; } public virtual Project Project { get; set; } public virtual ICollection Employee { get; set; } public virtual ICollection Activities { get; set; } public virtual ICollection Hours { get; set; } } [/code] And the view: [code] @using (Html.BeginForm()) { @Html.ValidationSummary(true) Hour @Html.LabelFor(model=> model.Activity.ProjectID,"Project")
@Html.DropDownListFor(model => model.Activity.Project.ProjectID, new SelectList(ViewBag.Projects as IEnumerable,"ProjectID","Name"),null, new {id ="project"}) @Html.ValidationMessageFor(model=> model.Activity.Project.Name)
@Html.LabelFor(model=> model.Activity.Name)
@Html.DropDownListFor(model => model.Activity, new SelectList(Enumerable.Empty(),"ActivityID", "Name"),null, new {id = "activity"}) @Html.ValidationMessageFor(model=> model.ActivityID)
[/code] And the controller: [code] public ActionResult Create() { //ViewBag.WeekID = new SelectList(db.Weeks, "WeekID", "WeekID"); //ViewBag.weeknr = new SelectList( "weeknr", "value"); //var query = db.Activities.Select(c => new {c.ActivityID, c.Name}); // ViewBag.Activities = new SelectList(query.AsEnumerable(), "ActivityID", "Name"); ViewBag.HourID = new SelectList(db.Hours, "HourID", "HourID"); //ViewBag.ProjectID = new SelectList(db.Projects, "ProjectID", "Name"); //ViewBag.ActivityID = new SelectList(query.AsEnumerable(), "ActivityID", "Name", 2); // ViewBag.ActivityID = new SelectList(db.Activities, "ActivityID", "Name"); ViewBag.Projects = db.Projects.ToList(); ViewBag.Activities = db.Activities.ToList(); /* IEnumerable items = db.Activities .Select(c => new SelectListItem { Value = c.ActivityID.ToString(CultureInfo.InvariantCulture), Text = c.Name }); ViewBag.Activities = items; */ return View(new Hour()); }//end method create [AcceptVerbs(HttpVerbs.Get)] public JsonResult GetProjectList(string id) { var ProjectList = this.GetProjects(Convert.ToInt32(id)); var myData = ProjectList.Select(p => new SelectListItem { Text = p.Name, Value = p.ActivityID.ToString(CultureInfo.InvariantCulture) }); return Json(myData, JsonRequestBehavior.AllowGet); }//end method private IEnumerable GetProjects(int id) { //List of activities corresponding with the selected Project return db.Activities.Where(p => p.ProjectID == id).ToList(); }//end method // // POST: /Hour/Create [HttpPost] public ActionResult Create(Hour hour) { try { if (ModelState.IsValid) { if (db.Hours != null) db.Hours.Add(hour); //db.Hours.Add(hour.Monday.Value.ToString("d")); db.SaveChanges(); return RedirectToAction("Index"); } } catch (DataException) { //ModelState.AddModelError("",hour.Monday.Value.ToShortDateString()); //ModelState.AddModelError("", hour.HourID.ToString()); // ModelState.AddModelError("Cant commit to database!! U forgot something??",hour.Activity.ActivityID.ToString()); ModelState.AddModelError("",hour.Activity.Project.ProjectID.ToString()); }//end catch exception. //ViewBag.WeekID = new SelectList (db.Weeks, "Monday", "Monday", hour.WeekID); // ViewBag.weeknr = new SelectList( "weeknr", "txtBox"); //ViewBag.HourID = new SelectList(db.Hours, "txtBox", "Monday"); //Important!! This will mangage that the actuall data will be saved.If u forget this then u only see it in the view!! if (db.Projects != null) { ViewBag.Projects = db.Projects.ToList(); } if (db.Activities != null) { ViewBag.Activities = db.Activities.ToList(); } return View(hour); }//end method // // GET: /Hour/Edit/5 [/code] THX for helping
albert albertPosted Jul 5, 2012, 6:03 AM
Because if I make an new project or activity I see the data in the dropdownlist. But I cant create new and it it show null??
How can that be?