I'm using the twitter bootstrap nuget package for MVC.
When posting to my controller, and checking for errors however, if I have more than one error in my model, I get the following error when trying to add a second alert to TempData:
An item with the same key has already been added.
//
// POST: /Customer/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Customer customer)
{
var errors = ModelState.Values.SelectMany(v => v.Errors);
if (ModelState.IsValid)
{
// save to database etc
// redirect to action
}
foreach (var c in errors)
{
TempData.Add(Alerts.ERROR, c.ErrorMessage); // Error is reported here
}
The error messages are difference, as shown below - so it must be that Alerts.ERROR is only allowed once:
Is there any way of adding two Alerts.Error
error messages to TempData
- or should I just concatenate a string, and add one error with combined error messages?
If I change the TempData code to:
TempData.Add(Alerts.ERROR, errors.Select(c => c.ErrorMessage).ToArray());
...the view renders as:
Thank you,
Mark