Ehsan Akbar
Ehsan Akbar

Reputation: 7301

javascript alert can't display its message in razor code

I am trying to show a message using alert javascript .

So in my controller i have this code :

[HttpPost]
        //[Authorize(Roles = "Admin")]
        public ActionResult Create(Student student)
        {

                    TempData["Message"] = "Product Updated";

                return RedirectToAction("Login", "Account");
        }

In my Login view that i want to show my message i have this :

<section id="loginForm" style="float:right;border:none">
<h2>نام کاربری و کلمه عبور را وارد کنید</h2>
@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
       //my login form
    </fieldset>

}
</section>

<section class="social" id="socialLoginForm">

</section>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

@if (TempData.ContainsKey("Message"))
{

    <script type="text/javascript">
        alert(@TempData["Message"]);
  </script>

}

But the message doesn't appear .why ?

Best regards

Upvotes: 0

Views: 5561

Answers (1)

Satpal
Satpal

Reputation: 133403

Wrap @TempData["Message"] in quotes. Otherwise it is considered as variable. You must be getting error in your browser console.

Use

<script type="text/javascript">
    alert('@TempData["Message"]');
</script>

Upvotes: 3

Related Questions