I have this: Controller
[code]
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ManyToMany.Models;
namespace ManyToMany.Controllers
{
public class HourController : Controller
{
private TimeSheetContext db = new TimeSheetContext();
//
// GET: /Hour/
public ViewResult Index()
{
var hours = db.Hours.Include(h => h.Week);
return View(hours.ToList());
}
[HttpPost]
public ActionResult Index(int year)
{
return new EmptyResult();
}
/*
public ActionResult WikiDetails(bool editable)
{
//Wiki wiki = new Wiki
Wiki wiki = new Wiki
{
Name = "Hallo",
Url = "hallo.com",
Editable = editable
};
return View(wiki);
}
*/
//
// GET: /Hour/Details/5
public ViewResult Details(int id)
{
Hour hour = db.Hours.Find(id);
return View(hour);
}
//
// GET: /Hour/Create
public ActionResult Create()
{
//ViewBag.WeekID = new SelectList(db.Weeks, "WeekID", "WeekID");
//ViewBag.weeknr = new SelectList( "weeknr", "value");
ViewBag.HourID = new SelectList(db.Hours, "HourID", "HourID");
return View(new Hour());
}
//
// POST: /Hour/Create
[HttpPost]
public ActionResult Create(Hour hour)
{
try
{
if (ModelState.IsValid)
{
db.Hours.Add(hour);
//db.Hours.Add(hour.Monday.Value.ToString("d"));
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch (DataException)
{
ModelState.AddModelError("",hour.Monday.ToString());
}
ViewBag.WeekID = new SelectList (db.Weeks, "Monday", "Monday", hour.WeekID);
// ViewBag.weeknr = new SelectList( "weeknr", "txtBox");
//ViewBag.HourID = new SelectList(db.Hours, "txtBox", "Monday");
return View(hour);
}//end method
//
// GET: /Hour/Edit/5
public ActionResult Edit(int id)
{
Hour hour = db.Hours.Find(id);
ModelState.AddModelError("Name", "What a shit name");
ViewBag.WeekID = new SelectList(db.Weeks, "WeekID", "WeekID", hour.WeekID);
return View(hour);
}
//
// POST: /Hour/Edit/5
[HttpPost]
public ActionResult Edit(Hour hour)
{
if (ModelState.IsValid)
{
db.Entry(hour).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.WeekID = new SelectList(db.Weeks, "WeekID", "WeekID", hour.WeekID);
return View(hour);
}
//
// GET: /Hour/Delete/5
public ActionResult Delete(int id)
{
Hour hour = db.Hours.Find(id);
return View(hour);
}
//
// POST: /Hour/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Hour hour = db.Hours.Find(id);
db.Hours.Remove(hour);
db.SaveChanges();
return RedirectToAction("Index");
}
/*
public DateTime getStartOfWeek(bool useSunday)
{
DateTime now = DateTime.Now;
int dayOfWeek = (int)now.DayOfWeek;
if (!useSunday)
dayOfWeek--;
if (dayOfWeek < 0)
{
dayOfWeek = 6;
}
return now.AddDays(-1 * (double)dayOfWeek);
}
*/
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
[/code]
Model:
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Globalization;
using System.Web.Mvc;
namespace ManyToMany.Models
{
public class Hour
{
public int HourID { get; set; }
public virtual Week Week { get; set; }
public int? WeekID { get; set; }
[ReadOnly(true)]
public decimal HourTotal { get; set; }
public int WeekNumber { get; set; }
public int yearNumber { get; set; }
public DayOfWeek DayWeek { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public DateTime? Monday { get; set; }
public int Days { get; set; }
public Decimal? MondayHours { get; set; }
public Hour()
{
}
}
}
[/code]
The View:
[code]
@using System.Collections
@using System.Globalization
@using System.Web.Mvc.Html
@using ManyToMany.Helpers
@using ManyToMany.Controllers
@model ManyToMany.Models.Hour
@{
ViewBag.Title = "Create";
}
Create
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
}
@Html.ActionLink("Back to List", "Index")
[/code]
And JQuery:
[code]
function Selectedchange() {
$("#Monday").val($("#weeknr").val().toString().split(",") [0]);
$("#Tuesday").val($("#weeknr").val().toString().split(",")[1]);
$("#wendsday").val($("#weeknr").val().toString().split(",")[2]);
$("#Thursday").val($("#weeknr").val().toString().split(",")[3]);
$("#Fryday").val($("#weeknr").val().toString().split(",")[4]);
$("#Saterday").val($("#weeknr").val().toString().split(",")[5]);
$("#Sunday").val($("#weeknr").val().toString().split(",")[6]);
};
[/code]
I get the correct value back if I select a value from the dropdownlist - in red. Because I put by the create method and exception:
[code]
[HttpPost]
public ActionResult Create(Hour hour)
{
try
{
if (ModelState.IsValid)
{
db.Hours.Add(hour);
//db.Hours.Add(hour.Monday.Value.ToString("d"));
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch (DataException)
{
ModelState.AddModelError("",hour.Monday.ToString());
}
ViewBag.WeekID = new SelectList (db.Weeks, "Monday", "Monday", hour.WeekID);
// ViewBag.weeknr = new SelectList( "weeknr", "txtBox");
//ViewBag.HourID = new SelectList(db.Hours, "txtBox", "Monday");
return View(hour);
}//end method
[/code]
But it doesnt committ it to the databse. I only can do it hardcoded, like this:
[code]
var hours = new List
{
new Hour() {WeekNumber = 22,Monday = DateTime.Parse("2012-09-01")},
new Hour() {WeekNumber = 23,Monday = DateTime.Parse("31-05-2012")},
new Hour() {WeekNumber = 23,Monday = DateTime.Parse("30-06-2012")},
};
hours.ForEach(e => context.Hours.Add(e));
context.SaveChanges();
[/code]
And the error message of the exception is:
{"An overflow occurred while converting to datetime."}
THX for helping!!
albert albertPosted Jun 6, 2012, 8:52 AM
{"An overflow occurred while converting to datetime."}??
I it is not a minium value of date.
If I hover over the property hour.Monday I get the correct value:
{13-2-2012 0:00:00}.
But it doesnt commit it to the database.