0

I have the following string in french languages which contains single quotes between the characters and words. This message I want to show in alert.

string Mlocal = "Ce porduit n'a pas encore été livré.";


TempData["msg_Vide"] = "<script>alert('" + Mlocal + "');</script>";

the alert not showing

i use escape "\'" and "&sbquo;" and "&#8218;" and "&#39;" but not working

passing string value is ok, but passing alert message with the special characters no ok

Mohamed Wardan
  • 116
  • 2
  • 8
  • 1
    Possible duplicate of [How do I escape a single quote ( ' ) in JavaScript?](https://stackoverflow.com/questions/16134910/how-do-i-escape-a-single-quote-in-javascript) – Hooman Bahreini Mar 09 '19 at 21:53

1 Answers1

1

Just pass the string to you view and let your view do the rest (and of course escape your single quote:

public IActionResult Index()
{
    TempData["msg_Vide"] = "Ce porduit n\\'a pas encore été livré.";
    return View();
}

View:

@if(TempData["msg_Vide"] != null) { 
    <script>
        alert('@Html.Raw(TempData["msg_Vide"])')
    </script>
}

enter image description here

Marco
  • 22,856
  • 9
  • 75
  • 124
  • i use in controller `if (Mlocal != null) { TempData["msg_Vide"] = ""; ViewBag.msg_Vide = Mlocal; } else { TempData["msg_Vide"] = ""; } ` and in view : `@Html.Raw(TempData["msg_Vide"])` **thank you is working now** – Mohamed Wardan Mar 09 '19 at 23:33