ASP.NET MVC 2.0
I'm doing Post-Redirect-Get, if I get errors on post, I need to include ModelErrors along for the ride to along -Redirect-Get route. I send it through 'TempData':
TempData["modelErors"] =
ModelState.
Where(item => item.Value.Errors.Count > 0).
ToDictionary(
item => item.Key,
item => item.Value.Errors.Select(error=>error.ErrorMessage).ToList()
);
And then reinsert it into a ModelState:
if (TempData.ContainsKey("modelErors")) {
foreach (var errors in (IDictionary<string,IList<string>>) TempData["modelErors"]) {
foreach (var error in errors.Value) {
ModelState.AddModelError(errors.Key, error);
}
}
}
Is there a better way?