Using Presentation AND ViewModel classes?

Ok, here’s a question for you mvc guys. Currently, I have actions similar to the one below.


 
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Edit(CodeEditModel model)
{
 
// let BaseController merge IValidationErrors into ModelState
ConsumeErrors(codeService.ValidateCodeEditModel(model));
 
if (ModelState.IsValid)
{
codeService.ProcessCodeEditModel(model);
return RedirectToAction("Manager");
}
 
return View(model);
 
// exceptions are caught by HandleError
 
}
 

It takes a model class as input and processes it, spitting it back to the view if it is not valid. The problem is, what I need to send to the view is much more complex that this. In addition, a lot of this other information isn’t really a part of the CodeController.

Before I moved to the ViewModel paradigm, I was using something more like the following:


 
public ActionResult Edit([Bind(Prefix = "Item")] Code entity)
{
 
// process the entity...and then if it fails...
 
// call BaseController method to fill in properties of the base ItemPresentation class
return PreparedView(new CodePresentation(entity));
 
}
 
public class CodePresentation : ItemPresentation<Code> { }
 

So my question is this: is it reasonable and in good form, to combine the two like the following?


 
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Prefix = "Item")] CodeEditModel model)
{
 
// let BaseController merge IValidationErrors into ModelState
ConsumeErrors(codeService.ValidateCodeEditModel(model));
 
if (ModelState.IsValid)
{
codeService.ProcessCodeEditModel(model);
return RedirectToAction("Manager");
}
 
return PreparedView(new CodeEditPresentation(model));
 
// exceptions are caught by HandleError
 
}
 

Here’s another problem I am trying to get around. In the case of a delete operation, in addition to displaying some info to help the user decide whether or not to proceed, they must also tick a confirmation box.

public class CodeDeleteModel
{
public int Id { get; set; }
public string Name { get; set; } // the name of the code
public bool Confirmed { get; set; }
}

Now this introduces a problem. On httppost, I don’t need the Name property, but if I don’t capture it in a hidden field, it won’t be displayed the next time around, should a validation error occur (like when they don’t tick the confirmation box).

public actionresult Delete(CodeDeleteModel model)
{
// process here and go back to manager, otherwise…
return View(model);
// if ‘Name’ is not in a hidden field, it isn’t re-displayed.
}

How do I get around having to do this without rebuilding the model?

If I do it like this following shows, it adds more code to the app:

// just the stuff I need on postback
public class CodeDeleteForm
{
public int Id { get; set; }
public bool Confirmed { get; set; }
}

// just the stuff I need on display
public class CodeDeletePage
{
public int Id { get; set; }
public string Name { get; set; }
}

public actionresult Delete(CodeDeleteForm form)
{
Code entity = codeService.GetCodeFromForm(form);
// process
return View(CodeDeletePage.FromEntity(entity));
}

As you can see, I am still quite confused by a lot of this.

What’s the correct way to do all this?