anyone have any solution?
controller Details
this is controller where multiple error message generated in for loop
public async Task<IActionResult> Register(RegisterViewModel model)
{
var result = await userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
var token = await userManager.GenerateEmailConfirmationTokenAsync(user);
var confirmationLink = Url.Action("ConfirmEmail", "AccountRoles",
new { userId = user.Id, token = token }, Request.Scheme);
}
foreach (var error in result.Errors)
{
TempData["msg"] = error.Description;
}
return View(model);
}
js
$(document).ready(function () {
var a = '@TempData["msg"]';
if (a != '')
toastr.error(a)
})
The for loop in your controller method does not generate an array of error messages.
The line TempData["msg"] = error.Description
is repeated in the loop which means that in your javascript calling @TempData["msg"]
will result in only retrieving the last error message.
You need to generate a list of error messages and then store them in TempData.
TempData["msg"] = result.Errors.Select(x => x.Description).ToList();
And then in javascript, you loop over the resulting array and display an error message for each one.
$(document).ready(function () {
var errors = @Html.Raw(Json.Serialize(TempData["msg"]));
errors.forEach(x => toastr.error(x));
});