Consider the following code:
public ActionResult Edit(int id)
{
return View(db.Foos.Single(x => x.Id == id));
}
When user submits the changes, I would like to receive both original and current object values, such that the Update code can be:
Foo foo = db.Foos.Attach(current, original);
db.SubmitChanges();
I see two options:
1) Render a number of hidden inputs containing original values
<input type="hidden" name="original.A" value="<%= Model.A %> />
<input type="hidden" name="original.B" value="<%= Model.B %> />
<input type="text" name="current.A" value="<%= Model.A %>
<input type="text" name="current.B" value="<%= Model.B %>
and submit to:
public ActionResult Update(Foo current, Foo original)
{
Foo foo = db.Foos.Attach(current, original);
db.SubmitChanges();
}
2) Use some serialization/deserialization into one hidden field
<input type="hidden" name="original" value="<%= Serialize(original) %> />
and sumbmit to:
public ActionResult Update(Foo current, string original)
{
Foo original = DeserializeFrom<Foo>(original);
Foo foo = db.Foos.Attach(current, original);
db.SubmitChanges();
}
Are there any other options? Or tools that make writing such code easier?
EDIT:
To be more clear... the idea of keeping original value is to eliminate extra select that happens if code written this way:
public ActionResult Update(Foo changed)
{
Foo original = db.Foos.Single(x => x.Id == changed.Id);
MyUtils.CopyProps(original, current);
db.SubmitChanges();
}