what i did in the project:
public interface IBuilder<TEntity, TInput> { TInput BuildInput(TEntity entity); TEntity BuildEntity(TInput input); TInput RebuildInput(TInput input); }
implement this interface for each object or /, and for some group of entities you can make a general one and use it in each controller; use IoC;
you put your mapping code in the first 2 methods (no matter what display technology you can even do it manually) and RebuildInput is when you get the ModelState.IsValid == false value, just call BuildEntity and BuildInput again.
and use in the controller:
public ActionResult Create() { return View(builder.BuildInput(new TEntity())); } [HttpPost] public ActionResult Create(TInput o) { if (!ModelState.IsValid) return View(builder.RebuildInput(o)); repo.Insert(builder.BuilEntity(o)); return RedirectToAction("index"); }
I really sometimes use a common controller, which is used for more objects like here: asp.net mvc general controller
EDIT: you can see this technique in the asp.net mvc example application here: http://prodinner.codeplex.com
source share