Hi everybody,
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";
}
<h2>
Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/SelectWeek.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/SelectTuesday.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Hour</legend>
<table style="width: 100%;">
<tr>
<td>
<div class="editor-field">
@Html.Hidden("WeekID", String.Empty)
@Html.ValidationMessageFor(model => model.WeekID)
</div>
</td>
<td>
<div class="editor-label">
@Html.LabelFor(model => model.Week.YearNumber, "jaar")
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Week.YearNumber,Html.GetWeekOfYear())
@Html.ValidationMessageFor(model => model.Week.YearNumber)
</div>
</td>
<td>
<div class="editor-label">
@Html.LabelFor(model => model.WeekNumber, "week")
</div>
<div class="editor-field">
@Html.DropDownList("weeknr", (IEnumerable<SelectListItem>)Html.GetWeekNumbers(), new { onchange = "Selectedchange();" })
@Html.ValidationMessage("week")
</div>
</td>
<td>
<div class="editor-label">
@Html.Label("Maandag")
</div>
<div class="editor-field">
@Html.TextBox("Monday", Model.Monday.ToString())
@Html.ValidationMessageFor(model=> model.Monday)
</div>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
</tr>
</table>
<p>
<input type="submit" value="Chicken" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
[/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<Hour>
{
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!!