Let's say I have a form with the following "business object" in mind:
public class MyObject
{
public string Name { get; set; }
public User OtherUser { get; set; }
public DateTime CreateDate { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
}
When the user enters data into any of the fields, I need to round-trip what they entered in order to allow them to correct any fat-fingerings and the like, without having to retype their response.
Because I cannot round-trip a value like 'Feb 31, 2011' in a DateTime field, I end up using an View Model object like this:
public class MyObjectViewModel
{
public string Name { get; set; }
public string OtherUserName { get; set; }
public string CreateDate { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
}
Ok, all is fine and dandy, I can get this to render exactly the same as the bare business object and the values round-trip when using the following pattern:
public ActionResult Create(FormCollection form)
{
var model = new MyObjectViewModel();
if (TryUpdateModel(model, form) && ModelState.IsValid)
{
// ...
}
return View(model);
}
I having the following questions:
- What is the cleanest way to get the data out of the View Model and into the Business Object? (i.e. What fills out the rest of the method above?)
- Can I reduce the duplication of field names between the objects, so as to consolidate my changes?
- In the above example, the field
OtherUserNameis astringthat needs to be converted into aUserobject. Whose responsibility is this? The Controller? The ViewModel? A model binder?