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
}