I'm still learning a lot about The Entity framework and how it communicates, so this is probably something simple, but I cannot seem to figure out what the problem is.
To set the scene, I have a working Create function generated by Visual Studio, and I have set the values in this function as follows:
myObject.Id = Guid.NewGuid().ToString();
myObject.dateCreated = DateTime.Now;
This looks great, and everything is working as it should. The problem comes when I then try to edit the record.
I have this model:
public class MyObject
{
public string Id { get; set; }
public string UserId { get; set; }
public string Name { get; set; }
public DateTime dateCreated { get; set; }
public Nullable<DateTime> dateModified { get; set; }
public Boolean aDefault { get; set; }
}
(For the most part auto generated) these controllers:
public async Task<ActionResult> Edit(string id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
MyObject myObject = await db.myObjects.FindAsync(id);
if (myObject == null)
{
return HttpNotFound();
}
return View(myObject);
}
public async Task<ActionResult> Edit([Bind(Include = "Id,aDefault,Name")] MyObject myObject)
{
if (ModelState.IsValid)
{
myObject.dateModified = DateTime.Now;
db.Entry(myObject).State = EntityState.Modified;
await db.SaveChangesAsync(); //db is the generated dbcontext
return RedirectToAction("Index");
}
return View(myObject);
}
And (only displaying the fields I have on the form) the view:
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.aDefault, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.aDefault)
@Html.ValidationMessageFor(model => model.aDefault, "", new { @class = "text-danger" })
</div>
</div>
</div>
So currently when I run this code and go to the edit area, I'm expecting it to take the Name and default values, post them to the controller, and have that only modify the relevant fields in the database. However, currently when I run this code, I get:
The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. The statement has been terminated.
This caused me to believe my dateCreated was being set to a null. I confirmed this by making it a Nullable and it no longer threw the exception, but of course set my dateCreated in the database to a null.
Do I need to bind all properties to the view similar to how the Id was set in a hidden field? I'd hope not as I figure I only need to bind the relevant items. Is there a way I can exclude these fields that are set once to not be updated?