Reputation: 437
In my application I have an Index page with a list of Documents. I want to add a new Document with an AJAX form on that same Index page. I am passing a NewDocument object to the Index page using a ViewModel.
The AJAX Form (I omitted some parts for readability):
@using (Ajax.BeginForm("Index_AddDocument", new AjaxOptions { UpdateTargetId = "MainList" }))
<div class="editor-field">
@Html.EditorFor(model => model.NewDocument.Date)
@Html.ValidationMessageFor(model => model.NewDocument.Date)
</div>
And here is the method that should save the new Document to the database, and update the Partial View list of documents:
public ActionResult Index_AddDocument(HomeIndexViewModel viewModel)
{
db.Documents.Add(viewModel.NewDocument);
db.SaveChanges();
return PartialView("ListOfDocs", db.Documents);
}
This doesn't work, I'm getting an exception at db.SaveChanges()
. It says:
InnerException {"An overflow occurred while converting to datetime."} System.Exception {System.Data.SqlServerCe.SqlCeException}
EDIT: My Document domain model has the following Date property:
[DataType(DataType.Date)]
public DateTime Date { get; set; }
In the AJAX form I just type a random date like 5/2/2005 and then click Submit, and then get the exception mentioned above.
Upvotes: 3
Views: 542
Reputation: 437
I think I just solved my own problem. The 'Date' column in my database is of the type 'datetime'. But my domain model had this:
[DataType(DataType.Date)]
public DateTime Date { get; set; }
I changed that DataType to 'DataType.DateTime' and now it seems to be working fine.
Thanks everyone for the guidance!
Upvotes: 1
Reputation: 6834
What is the value of viewModel.NewDocument.Date
prior to the db.SaveChanges()
call?
C# can handle dates from 1st Jan 0000 to 31st Dec 9999. I know SQL Server can't handle that whole range (e.g. SQL Server 2008 can't handle before 1/1/1753 i believe)
I suspect that SQL Server CE also has limits on its date range and is balking at the default 1/1/0000 DateTime value.
Upvotes: 1