I have this:
Model:
[code]
public class Hour
{
public int HourID { get; set; }
public int HoursPerDay { get; set; }
//private DateTime monday;
[DataType(DataType.Date)]
public DateTime Monday { get; set; }
public int Days { get; set; }
public Hour(/*DateTime Monday*/)
{
Days = DateTime.Now.DayOfWeek - DayOfWeek.Monday;
Monday = DateTime.Now.AddDays(-Days);
}
}
[/code]
class week:
[code]
public class Week
{
public int WeekID { get; set; }
//private DateTime year;
//[DisplayFormat(DataFormatString = "{0:yyyy}",ApplyFormatInEditMode = true)]
//public DateTime Date { get; set; }
public string Name { get; set; }
[DataType(DataType.Date)]
public int? WeekNumber { get; set; }
public virtual IEnumerable
//DateTime date1 = new DateTime(2011, 1, 1);
//public t MyProperty { get; set; }
TimeSheetContext _context = new TimeSheetContext();
public int SelectID { get; set; }
public Week(/*int YearNumber*/)
{
YearNumber = DateTime.Now.Year;// .AddYears(0);
}
public DateTime WeekNumberss { get; set; }
//[DataType(DataType.Date)]
public int YearNumber { get; set; }
}
[/code]
the View:
[code]
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
}
@Html.ActionLink("Back to List", "Index")
[/code]
and the controller:
[code]
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");
return View(new Hour());
}
//
// POST: /Hour/Create
[HttpPost]
public ActionResult Create(Hour hour)
{
if (ModelState.IsValid)
{
db.Hours.Add(hour);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.WeekID = new SelectList(db.Weeks, "WeekID", "WeekID", hour.WeekID);
return View(hour);
}
//
// 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]
and the extension method for the dropdownlist for week numbers:
[code]
public static IEnumerable
{
var janFirst = new DateTime(DateTime.Today.Year, 1, 1);
//beware different cultures.
var firstWeek = janFirst.AddDays(1 - (int)(janFirst.DayOfWeek));
int j = -1;
foreach (var weekNumber in Enumerable.Range(0, 52).Select(i => new
{
weekStart = firstWeek.AddDays(i * 7),
//Tuesday = firstWeek.AddDays(i * 3)
}).TakeWhile(x => x.weekStart.Year <= janFirst.Year).Select(x => new
{
x.weekStart,
weekFinish = x.weekStart.AddDays(6)
}).SkipWhile(x => x.weekFinish < janFirst.AddDays(1)))
{
j++;
yield return new SelectListItem
{
Value = string.Join(",",new string[]
{
weekNumber.weekStart.ToShortDateString()
// weekNumber.weekStart.AddDays(1),
// weekNumber.weekStart.AddDays(2),
// weekNumber.weekStart.AddDays(3),
// weekNumber.weekStart.AddDays(4),
// weekNumber.weekStart.AddDays(5),
// weekNumber.weekStart.AddDays(6),
}),
Text = (j + 1).ToString(CultureInfo.InvariantCulture)
};
}
}//end method
[/code]
I 've also included the hole project.
but when I try to save the data I get this error:
Server Error in '/' Application.
An overflow occurred while converting to datetime.
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.Data.SqlServerCe.SqlCeException: An overflow occurred while converting to datetime.
Source Error:
| Line 80: { Line 81: db.Hours.Add(hour); Line 82: db.SaveChanges(); Line 83: return RedirectToAction("Index"); Line 84: } |
THX for helping..
albert albertPosted May 29, 2012, 9:24 AM
Create
albert albertPosted May 29, 2012, 9:11 AM
[code]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public DateTime? Monday { get; set; }
[/code]
nothing is changing.
But if I select a value in the dropdownlist, for example first week of year 2012.
then in the textbox it shows the correct data:
but if I press on the create link it returns like this:
albert albertPosted May 29, 2012, 8:53 AM
ModelState.AddModelError("",hour.Monday.ToString());
I get this as output:
so it uses slashes instead of ''-'.
Becuase it has to be:
28-5-2012
albert albertPosted May 29, 2012, 8:43 AM
[code]
[HttpPost]
public ActionResult Create(Hour hour)
{
try
{
if (ModelState.IsValid)
{
db.Hours.Add(hour);
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch (DataException)
{
ModelState.AddModelError("","Cant commit");
}
ViewBag.WeekID = new SelectList(db.Weeks, "WeekID", "WeekID", hour.WeekID);
return View(hour);
}
[/code]
albert albertPosted May 29, 2012, 8:34 AM
[code]
[HttpPost]
public ActionResult Create(Hour hour)
{
if (ModelState.IsValid)
{
db.Hours.Add(hour);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.WeekID = new SelectList(db.Weeks, "WeekID", "WeekID", hour.WeekID);
return View(hour);
}
[/code]
Jignesh TrivediPosted May 29, 2012, 8:26 AM
you can also use
Monday.Value.ToString(CultureInfo.CurrentCulture)
okay..
In your code debug "Create" Action method and verified that what value comes in Property called "Monday" it must be some valid date.
albert albertPosted May 29, 2012, 8:19 AM
[code]
@Html.Label("Maandag")
@Html.MyLabel("txtBox", Model.Monday)
@Html.ValidationMessageFor(model => model.Monday)
[/code]
and the property Monday to this:
[code]
[DataType(DataType.Date)]
public DateTime? Monday { get; set; }
[/code]
but It is still saying:
Server Error in '/' Application.
An overflow occurred while converting to datetime.
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.Data.SqlServerCe.SqlCeException: An overflow occurred while converting to datetime.
Source Error:
Line 80: { Line 81: db.Hours.Add(hour); Line 82: db.SaveChanges(); Line 83: return RedirectToAction("Index");I work with the entiyFramework. So the classes Hour and Week are also the Database tables.
albert albertPosted May 29, 2012, 8:11 AM
[code]
@Html.Label("Maandag")
@Html.MyLabel("txtBox", Model.Monday.ToString(CultureInfo.CurrentCulture))
@Html.ValidationMessageFor(model => model.Monday)
[/code]
but I have I try this:
[code]
[DataType(DataType.Date)]
public DateTime? Monday { get; set; }
[/code]
Then I get this error:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.Compiler Error Message: CS1501: No overload for method 'ToString' takes 1 arguments
Source Error:
Jignesh TrivediPosted May 29, 2012, 7:43 AM
If model has non-nullable property of type DateTime and you post a form with empty value of that property, system (mvc ValueProvider) automatically set to DateTime.MinValue, which is in .net 01/01/0001
also share db structure because min value of SQL datetime is 01/01/1753.
If you use SQL server 2008 then use datatime2
hope this will help.