Reputation: 98
I am trying to pass an error message through the ModelState dictionary to be fed to a javascript code that generates an alert with that error. Everything works, but I can't get the special characters to appear correctly like " ' " and "è". Solutions?
Code AspNet Core:
ModelState.AddModelError("Utente", "L'utente per cui si tenta di reimpostare la password non è ancora registrato");
Code JS:
<script type="text/javascript">
@if (!ViewData.ModelState.IsValid && ViewData.ModelState.ContainsKey("Utente"))
{
@if( ViewData.ModelState["Utente"].Errors.Count > 0)
{
<text>
$(document).ready(function() {
alert('@ViewData.ModelState["Utente"].Errors.First().ErrorMessage');
});
</text>
}
}
Upvotes: 1
Views: 1139
Reputation: 18973
You can use double quote instead of single quote in message variable
$(document).ready(function() {
var message = "@Html.Raw(ViewData.ModelState["Utente"].Errors.First().ErrorMessage)";
alert(message);
});
Upvotes: 0
Reputation: 82241
You can use @Html.Raw()
to prevent encoding:
alert("@Html.Raw(ViewData.ModelState["Utente"].Errors.First().ErrorMessage)");
Upvotes: 2