How to display a Javascript message in an Asp.net MVC view?

Asked

Viewed 2,259 times

1

I have this code:

@if (!string.IsNullOrWhiteSpace (Model.ErrorMessage))
{
     <Script>
         $ ("#ModalError').modal.('show.');
     </Script>

     Response.Write("<script>alert('Olá');</script>");

     HttpContext.Current.Response.Write("<script>alert('Olá');</script>");
}

Where I check if Model.Error message is other than empty, so I wanted to give an alert to the user displaying the error message, but none of the way it’s in the middle of this condition if it’s working.

I’ve tried that too:

@if (!String.IsNullOrEmpty(ViewData["erro"] as string))
{
    <script>alert(@ViewData["erro"]);</script>

}

This is a part of the view.

My controller is like this:

public ActionResult Login(LoginViewModel model, SignInMessage message)
        {
            if (!String.IsNullOrEmpty(model.ErrorMessage))
                ViewData["erro"] = !String.IsNullOrEmpty(model.ErrorMessage) ? model.ErrorMessage : null;

            return this.View(model);
        }

Does anyone know how to solve?

  • You tried with @Html.Raw("<script>Alert("+ Viewdata["error"] as string +");</script>"); ?

  • I tested and nothing came up.

  • missing simple quotes @Html.Raw("<script>Alert('"+ Viewdata["error"] string +"');</script>");

4 answers

2


Friend the error is that you have to define an event to that action I think the correct would be in the ready of the page !

Well I guess that’s what to be sure I’d need to see the project !

I hope I helped. Good luck there !

$(document).ready(function(){
    $('#modalError').modal();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
     <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<!DOCTYPE html>
<html>
<head>
    <title>exemple</title> 
     <meta charset="utf-8">
</head>
<body>

<div id="modalError" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <div class="modal-body">
                <p>Message.</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

</body>
</html>

  • I can’t believe this is all.

  • hahahaha happens. .. problem solved ?

0

Have you tried using

@Html.Raw("<script>............</script>");

?

0

I did so and it worked.

Controler:

public class MensagemController : Controller
{
    // GET: Mensagem
    public ActionResult Index()
    {
        ViewData["erro"] = "Mensagem de Erro";

        return View();
    }
}

View:

@Html.Raw("<script>alert('" + ViewData["erro"] as string + "');</script>")

0

With Viewbag:

In the View, put:

@if (!string.IsNullOrWhiteSpace(ViewBag.Erro))
{
    <script>alert('Olá');</script>
}

No Controller:

public ActionResult Login(LoginViewModel model, SignInMessage message)
{
  ViewBag.Erro = model.ErrorMessage == null ? "" : model.ErrorMessage;
  return View();
}

With Model:

In the View, put:

@model NamespaceCompletodaModel.LoginViewModel 

@if (!string.IsNullOrWhiteSpace(Model.ErrorMessage))
{
    <script>alert('Olá');</script>
}

No Controller:

public ActionResult Login(LoginViewModel model, SignInMessage message)
{
  model.ErrorMessage = model.ErrorMessage == null ? "" : model.ErrorMessage;
  return View(model);
}

Cannot use Response via View.

  • I’ve tried exactly the first way you reported it, and it still doesn’t work.

  • I have to return a model inside the view, if not from the undefined object reference, but even using this code and passing the model in the view not right, I did this: @if (!string.Isnullorempty(Viewbag.Error)) { <script> Alert('Hello'); Alert(@Model.Errormessage); console.log(@Model.Errormessage); </script> } In view and not

  • Sorry. I made a correction.

  • I’ve tested it, it won’t

  • If you put only "<script>Alert('Hello');</script>" in the view and "Return View();" in the controller, you go?

  • Neither, and I can’t return a view without passing the model

  • My code is exactly like this.

  • How you trigger the Login method?

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.